From patchwork Tue Jun 22 08:49:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Ada] Bad bounds checks on slice of explicit dereference Date: Mon, 21 Jun 2010 22:49:40 -0000 From: Arnaud Charlet X-Patchwork-Id: 56416 Message-Id: <20100622084940.GA3712@adacore.com> To: gcc-patches@gcc.gnu.org Cc: Thomas Quinot 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 * sem_res.adb (Resolve_Slice): When the prefix is an explicit dereference, construct actual subtype of designated object to generate proper bounds checks. 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