This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Length of empty array with bounds overflowing result type


This patch removes an unwanted early conversion of range bounds to the
result type when testing whether a range is empty.  The extra conversion
may overflow and cause the check for empty range to return an incorrect
result, like in the attached testcase.

Tested on i586-suse-linux, applied on the mainline.


2008-11-07  Thomas Quinot  <quinot@adacore.com>

	* gcc-interface/trans.c (Attribute_to_gnu, case Attr_Length): Check
	for empty range in original base type, not converted result type.


2008-11-07  Thomas Quinot  <quinot@adacore.com>

	* gnat.dg/hyper_flat.adb: New test.


-- 
Eric Botcazou
Index: gcc-interface/trans.c
===================================================================
--- gcc-interface/trans.c	(revision 141668)
+++ gcc-interface/trans.c	(working copy)
@@ -1287,7 +1287,10 @@ Attribute_to_gnu (Node_Id gnat_node, tre
 		   much rarer cases, for extremely large arrays we expect
 		   never to encounter in practice.  In addition, the former
 		   computation required the use of potentially constraining
-		   signed arithmetic while the latter doesn't.  */
+		   signed arithmetic while the latter doesn't. Note that the
+		   comparison must be done in the original index base type,
+		   otherwise the conversion of either bound to gnu_compute_type
+		   may overflow.  */
 		
 		tree gnu_compute_type = get_base_type (gnu_result_type);
 
@@ -1301,7 +1304,9 @@ Attribute_to_gnu (Node_Id gnat_node, tre
 		gnu_result
 		  = build3
 		    (COND_EXPR, gnu_compute_type,
-		     build_binary_op (LT_EXPR, gnu_compute_type, hb, lb),
+		     build_binary_op (LT_EXPR, get_base_type (index_type),
+				      TYPE_MAX_VALUE (index_type),
+				      TYPE_MIN_VALUE (index_type)),
 		     convert (gnu_compute_type, integer_zero_node),
 		     build_binary_op
 		     (PLUS_EXPR, gnu_compute_type,
-- { dg-do run }
-- { dg-options "-gnatp" }

procedure Hyper_Flat is

   type Unsigned is mod 2 ** 32;
   x : Integer := 0;
   pragma Volatile (X);

   S : constant String := (1 .. X - 3 => 'A');
   --  Hyper-flat null string

begin
   if Unsigned'(S'Length) /= 0 then
      raise Program_Error;
   end if;
end;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]