diff mbox series

[Ada] Prevent infinite recursion when there is no expected unit

Message ID 20210708135015.GA2465724@adacore.com
State New
Headers show
Series [Ada] Prevent infinite recursion when there is no expected unit | expand

Commit Message

Pierre-Marie de Rodat July 8, 2021, 1:50 p.m. UTC
The comment in Par.Load says "... or we are in big trouble, and abandon
the compilation", but the code merely emitted errors and kept going. Now
it emits errors, flags the problem in the unit table and gives up. Also,
it was wrong for this routine to remove the unit, because the callers
who add entries to the unit table assume those entries to be filled by
the parser and not removed, even when irrecoverable errors happen.

This prevents an infinite recursion that happened when parsing a file
with multiple compilation units and wrong indexes, so the compiler was
scanning unit X, followed its WITH Y clause but instead of unit Y it was
getting unit X and scanned it again and again...

Also, it fixes a crash when compiling a program with subunit that
contains unexpected program unit (previously the compiler only cared
about avoiding such a crash with -gnatc switch).

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

gcc/ada/

	* par-load.adb (Load): Don't remove unit, but flag it as
	erroneous and return.
diff mbox series

Patch

diff --git a/gcc/ada/par-load.adb b/gcc/ada/par-load.adb
--- a/gcc/ada/par-load.adb
+++ b/gcc/ada/par-load.adb
@@ -234,9 +234,10 @@  begin
          Error_Msg ("\\found unit $!", Loc);
       end if;
 
-      --  In both cases, remove the unit so that it is out of the way later
+      --  In both cases, flag the fatal error and give up
 
-      Remove_Unit (Cur_Unum);
+      Set_Fatal_Error (Cur_Unum, Error_Detected);
+      return;
    end if;
 
    --  If current unit is a body, load its corresponding spec