From: Denis Mazzucato <[email protected]>

This patch fixes the crash that happens when emitting the error message of an
invalid warning switch with trailing spaces.

gcc/ada/ChangeLog:

        * sem_prag.adb (Analyze_Pragma): Add enclosing quotation when the
        invalid switch ends with a space.

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

---
 gcc/ada/sem_prag.adb | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/sem_prag.adb b/gcc/ada/sem_prag.adb
index 6b38de037bf..59c1976dbe9 100644
--- a/gcc/ada/sem_prag.adb
+++ b/gcc/ada/sem_prag.adb
@@ -28770,6 +28770,17 @@ package body Sem_Prag is
                         OK  : Boolean;
                         Chr : Character;
 
+                        function Enclose_Ending_Space
+                           (Raw_Str : String) return String
+                        is (if Raw_Str (Raw_Str'Last) = ' '
+                              then '"' & Raw_Str & '"'
+                              else Raw_Str);
+                        function Enclose_Ending_Space
+                           (Raw_Chr : Character) return String
+                        is (Enclose_Ending_Space ((1 => Raw_Chr)));
+                        --  This function ensures that no error message ends
+                        --  with a space, in case we enclose it within quotes.
+
                      begin
                         J := 1;
                         while J <= Len loop
@@ -28801,7 +28812,8 @@ package body Sem_Prag is
                                  if not Set_Warning_Switch ('.', Chr) then
                                     Error_Pragma_Arg
                                       ("invalid warning switch character "
-                                       & '.' & Chr, Arg1);
+                                       & Enclose_Ending_Space ('.' & Chr),
+                                       Arg1);
                                  end if;
 
                               --  Non-Dot case
@@ -28812,7 +28824,8 @@ package body Sem_Prag is
 
                               if not OK then
                                  Error_Pragma_Arg
-                                   ("invalid warning switch character " & Chr,
+                                   ("invalid warning switch character "
+                                    & Enclose_Ending_Space (Chr),
                                     Arg1);
                               end if;
 
-- 
2.51.0

Reply via email to