[Ada] Bad bounds checks on slice of explicit dereference

Arnaud Charlet charlet@adacore.com
Tue Jun 22 09:40:00 GMT 2010


For a slice whose prefix is an explicit dereference of an access to
unconstrained array, the bounds of the slice must be checked against the
actual bounds of the designated object, not the bounds of the index subtype,
as was the case prior to this change.

The following test must compile and execute silently (i.e. Constraint_Error
must be raised at the indicated line):

with Ada.Text_IO; use Ada.Text_IO;
procedure Bounds_Check_Slice_Of_Deref is
   type A is array (Positive range <>) of Integer;
   type AA is access all A;
   X  : AA := new A'(1 .. 2 => 0);
   Y : A (1 .. 3);
begin
   begin
      Y := X.all (1 .. 3); --  RAISE Constraint_Error;
      Put_Line ("FAIL");
   exception
      when Constraint_Error =>
         null;
   end;
end Bounds_Check_Slice_Of_Deref;

Tested on x86_64-pc-linux-gnu, committed on trunk

2010-06-22  Thomas Quinot  <quinot@adacore.com>

	* sem_res.adb (Resolve_Slice): When the prefix is an explicit
	dereference, construct actual subtype of designated object to generate
	proper bounds checks.

-------------- next part --------------
Index: sem_res.adb
===================================================================
--- sem_res.adb	(revision 161147)
+++ sem_res.adb	(working copy)
@@ -8105,6 +8105,7 @@ package body Sem_Res is
          end if;
 
       elsif Is_Entity_Name (Name)
+        or else Nkind (Name) = N_Explicit_Dereference
         or else (Nkind (Name) = N_Function_Call
                   and then not Is_Constrained (Etype (Name)))
       then


More information about the Gcc-patches mailing list