[Ada] Fix null dereference in loop at -O or above

Message ID 201010251918.06452.ebotcazou@adacore.com
State New
Headers show

Commit Message

Eric Botcazou Oct. 25, 2010, 5:18 p.m.
This fixes a crash at run time caused by a dereference of the null pointer.
The problem is that we put both TREE_READONLY and TREE_THIS_NOTRAP on the 
INDIRECT_REF built to access the bounds of fat pointer types.  While this 
fine if it is later instantiated for an unconstrained array, this isn't if it 
is later instantiated for a pointer to unconstrained array, as the pointer 
can be null and LIM will hoist everything out of loops, removing any guard 
before the dereference in the process.  So the conservative fix is not to put 

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

2010-10-25  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Array_Type>: Do not set
	TREE_THIS_NOTRAP on the INDIRECT_REF node built for the template.

2010-10-25  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/loop_optimization8.adb: New test.
	* gnat.dg/loop_optimization8_pkg1.ad[sb]: New helper.
	* gnat.dg/loop_optimization8_pkg2.ad[sb]: Likewise.


Index: gcc-interface/decl.c
--- gcc-interface/decl.c	(revision 165914)
+++ gcc-interface/decl.c	(working copy)
@@ -1942,7 +1942,6 @@  gnat_to_gnu_entity (Entity_Id gnat_entit
 	  = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
 	TREE_READONLY (gnu_template_reference) = 1;
-	TREE_THIS_NOTRAP (gnu_template_reference) = 1;
 	/* Now create the GCC type for each index and add the fields for that
 	   index to the template.  */