diff mbox series

[Ada] Fix missing array bounds checking

Message ID 20210616084355.GA94977@adacore.com
State New
Headers show
Series [Ada] Fix missing array bounds checking | expand

Commit Message

Pierre-Marie de Rodat June 16, 2021, 8:43 a.m. UTC
For an assignment statement of the form "A.B(C).D := ...", in a loop,
the index check on C can be missing.

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

gcc/ada/

	* ghost.adb: Add another special case where full analysis is
	needed. This bug is due to quirks in the way
	Mark_And_Set_Ghost_Assignment works (it happens very early,
	before name resolution is done).
diff mbox series

Patch

diff --git a/gcc/ada/ghost.adb b/gcc/ada/ghost.adb
--- a/gcc/ada/ghost.adb
+++ b/gcc/ada/ghost.adb
@@ -1245,11 +1245,21 @@  package body Ghost is
       --  processing them in that mode can lead to spurious errors.
 
       if Expander_Active then
+         --  Cases where full analysis is needed, involving array indexing
+         --  which would otherwise be missing array-bounds checks:
+
          if not Analyzed (Orig_Lhs)
-           and then Nkind (Orig_Lhs) = N_Indexed_Component
-           and then Nkind (Prefix (Orig_Lhs)) = N_Selected_Component
-           and then Nkind (Prefix (Prefix (Orig_Lhs))) =
-           N_Indexed_Component
+           and then
+             ((Nkind (Orig_Lhs) = N_Indexed_Component
+                and then Nkind (Prefix (Orig_Lhs)) = N_Selected_Component
+                and then Nkind (Prefix (Prefix (Orig_Lhs))) =
+                           N_Indexed_Component)
+              or else
+             (Nkind (Orig_Lhs) = N_Selected_Component
+              and then Nkind (Prefix (Orig_Lhs)) = N_Indexed_Component
+              and then Nkind (Prefix (Prefix (Orig_Lhs))) =
+                         N_Selected_Component
+              and then Nkind (Parent (N)) /= N_Loop_Statement))
          then
             Analyze (Orig_Lhs);
          end if;