Bug 42974 - Array slice assignment is missing bounds checking for the source array
Summary: Array slice assignment is missing bounds checking for the source array
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: ada (show other bugs)
Version: unknown
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-05 15:23 UTC by Tero Koskinen
Modified: 2011-06-16 17:18 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tero Koskinen 2010-02-05 15:23:22 UTC
GNAT does not generate dynamic/runtime bounds checking for array slice assignment.

Following code should raise exception (but it doesn't):
-- gnatmake -gnatf -gnato testcase.adb
procedure Testcase is
   type Element_Access is access all Integer;
   type Element_Array is
     array (Positive range <>) of Integer;

   type Element_Array_Access is access all Element_Array;

   A : Element_Array_Access := null;
   B : Element_Array_Access := null;
begin
   A := new Element_Array (1 .. 10);
   A.all := (1,2,3,4,5,6,7,8,9,10);
   B := new Element_Array (Positive'First .. Positive'First + 20);
   B.all (Positive'First .. Positive'First + 15)
     := A.all (A.all'First .. A.all'First + 15);
end Testcase;


Two other commercial compilers raise exception when the program is run, but GNAT doesn't.

Tested versions:
GNAT 4.3.2 on Debian GNU/Linux 5.0/stable (i386)
GNAT 4.4.2 on Fedora 12 (i386)
GNAT 4.2.4 on Ubuntu 8.04 (i386)
GNAT GPL 2009 on Windows XP (i386)

Example output from those commercial compilers:
-- first compiler
[C:\koodi\ada\buffer]testcase
** Unhandled CONSTRAINT_ERROR
   Index or Subtype out of bounds - Pos of Error Value =  16
On Line Number 16 In TESTCASE

[C:\koodi\ada\buffer]
-- second compiler
[C:\koodi\ada\buffer]testcase

Ada-runtime: Exception CONSTRAINT_ERROR raised in c:/koodi/ada/buffer/testcase.adb on line 16.
Bad bounds: 1..16 not in 1..10.

[C:\koodi\ada\buffer]
Comment 1 Tero Koskinen 2011-06-16 17:18:12 UTC
This is fixed in GCC 4.6.0 and GNAT GPL 2011.

I don't know which commit fixed this, but I can no longer repeat the problem with 4.6.0 or GPL 2011.

Resolving as FIXED.