]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: prevent infinite recursion in Collect_Types_In_Hierarchy
authorBob Duff <duff@adacore.com>
Mon, 30 Jan 2023 16:25:08 +0000 (11:25 -0500)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 22 May 2023 08:44:07 +0000 (10:44 +0200)
In (illegal) mutually-dependent type declarations, it is possible for
Etype (Etype (Typ)) to point back to Typ. This patch stops the recursion
in such cases.

gcc/ada/

* sem_util.adb (Process_Type): Stop the recursion.
* exp_aggr.adb (Build_Record_Aggr_Code): Add assertion.

gcc/ada/exp_aggr.adb
gcc/ada/sem_util.adb

index fe61e0ec90bf47ccad28d5661d3f6d881e43efff..58831bd51cad8f8f3ba870c605db8af5044a3680 100644 (file)
@@ -3837,6 +3837,7 @@ package body Exp_Aggr is
       Comp := First (Component_Associations (N));
       while Present (Comp) loop
          Selector := Entity (First (Choices (Comp)));
+         pragma Assert (Present (Selector));
 
          --  C++ constructors
 
index 1d8d4fc30f81667bbe240107c9150cffc5fa15d7..9cf21953fea77abadc3fc09b8fea185ab6ae30fe 100644 (file)
@@ -6235,6 +6235,19 @@ package body Sem_Util is
          --  Examine parent type
 
          if Etype (Typ) /= Typ then
+            --  Prevent infinite recursion, which can happen in illegal
+            --  programs. Silently return if illegal. For now, just deal
+            --  with the 2-type cycle case. Larger cycles will get
+            --  SIGSEGV at compile time from running out of stack.
+
+            if Etype (Etype (Typ)) = Typ then
+               if Total_Errors_Detected = 0 then
+                  raise Program_Error;
+               else
+                  return;
+               end if;
+            end if;
+
             Process_Type (Etype (Typ));
          end if;