]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix crash on Compile_Time_Warning in dead code
authorBob Duff <duff@adacore.com>
Wed, 31 Jan 2024 14:30:06 +0000 (09:30 -0500)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 13 May 2024 08:03:33 +0000 (10:03 +0200)
If a pragma Compile_Time_Warning triggers, and the pragma
is later removed because it is dead code, then the compiler
can return a bad exit code. This causes gprbuild to report
"*** compilation phase failed".

This is because Total_Errors_Detected, which is declared as Nat,
goes negative, causing Constraint_Error. In assertions-off mode,
the Constraint_Error is not detected, but the compiler nonetheless
reports a bad exit code.

This patch prevents that negative count.

gcc/ada/

* errout.adb (Output_Messages): Protect against the total going
negative.

gcc/ada/errout.adb

index d28a410f47b7dc7db700e4fccbd5a37f69ab66c2..c4761bd1bc9e8c2a98eb64893843fef2bbd59a84 100644 (file)
@@ -3399,11 +3399,16 @@ package body Errout is
 
       if Warning_Mode = Treat_As_Error then
          declare
-            Compile_Time_Pragma_Warnings : constant Int :=
+            Compile_Time_Pragma_Warnings : constant Nat :=
                Count_Compile_Time_Pragma_Warnings;
-         begin
-            Total_Errors_Detected := Total_Errors_Detected + Warnings_Detected
+            Total : constant Int := Total_Errors_Detected + Warnings_Detected
                - Warning_Info_Messages - Compile_Time_Pragma_Warnings;
+            --  We need to protect against a negative Total here, because
+            --  if a pragma Compile_Time_Warning occurs in dead code, it
+            --  gets counted in Compile_Time_Pragma_Warnings but not in
+            --  Warnings_Detected.
+         begin
+            Total_Errors_Detected := Int'Max (Total, 0);
             Warnings_Detected :=
                Warning_Info_Messages + Compile_Time_Pragma_Warnings;
          end;