diff mbox series

[COMMITTED] ada: Fix crash on Compile_Time_Warning in dead code

Message ID 20240513083636.165968-1-poulhies@adacore.com
State New
Headers show
Series [COMMITTED] ada: Fix crash on Compile_Time_Warning in dead code | expand

Commit Message

Marc Poulhiès May 13, 2024, 8:36 a.m. UTC
From: Bob Duff <duff@adacore.com>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/errout.adb | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/gcc/ada/errout.adb b/gcc/ada/errout.adb
index d28a410f47b..c4761bd1bc9 100644
--- a/gcc/ada/errout.adb
+++ b/gcc/ada/errout.adb
@@ -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;