]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Enforce alignment constraint for large Object_Size clauses
authorEric Botcazou <ebotcazou@adacore.com>
Thu, 12 Jun 2025 20:31:06 +0000 (22:31 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Thu, 3 Jul 2025 08:16:30 +0000 (10:16 +0200)
The constraint is that the Object_Size must be a multiple of the alignment
in bits.  But it's enforced only when the value of the clause is lower than
the Value_Size rounded up to the alignment in bits, not for larger values.

gcc/ada/ChangeLog:

* gcc-interface/decl.cc (gnat_to_gnu_entity): Use default messages
for errors reported for Object_Size clauses.
(validate_size): Give an error for stand-alone objects of composite
types if the specified size is not a multiple of the alignment.

gcc/ada/gcc-interface/decl.cc

index 1d9832d69ad4b39032269f5ee9e43d588e7d7ee8..27d2cea1f3d2a4adf945c3532a7378d011f6f154 100644 (file)
@@ -4502,7 +4502,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
          if (Known_Esize (gnat_entity))
            gnu_size
              = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
-                              VAR_DECL, false, false, size_s, type_s);
+                              VAR_DECL, false, false, NULL, NULL);
 
          /* ??? The test on Has_Size_Clause must be removed when "unknown" is
             no longer represented as Uint_0 (i.e. Use_New_Unknown_Rep).  */
@@ -9696,6 +9696,20 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
       return NULL_TREE;
     }
 
+  /* The size of stand-alone objects is always a multiple of the alignment,
+     but that's already enforced for elementary types by the front-end.  */
+  if (kind == VAR_DECL
+      && !component_p
+      && RECORD_OR_UNION_TYPE_P (gnu_type)
+      && !TYPE_FAT_POINTER_P (gnu_type)
+      && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
+                                    bitsize_int (TYPE_ALIGN (gnu_type)))))
+    {
+      post_error_ne_num ("size for& must be multiple of alignment ^",
+                        gnat_error_node, gnat_object, TYPE_ALIGN (gnu_type));
+      return NULL_TREE;
+    }
+
   return size;
 }