diff mbox series

[COMMITTED,09/30] ada: Fix incorrect String lower bound in gnatlink

Message ID 20240613133338.1809385-9-poulhies@adacore.com
State New
Headers show
Series [COMMITTED,01/30] ada: Missing dynamic predicate checks | expand

Commit Message

Marc Poulhiès June 13, 2024, 1:33 p.m. UTC
From: Ronan Desplanques <desplanques@adacore.com>

This patch fixes code in gnatlink that incorrectly assumed that the
lower bound of a particular string was always 1.

gcc/ada/

	* gnatlink.adb (Gnatlink): Fix incorrect lower bound assumption.
	(Is_Prefix): New function.

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

---
 gcc/ada/gnatlink.adb | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/gcc/ada/gnatlink.adb b/gcc/ada/gnatlink.adb
index 1455412ef93..db0fd144a13 100644
--- a/gcc/ada/gnatlink.adb
+++ b/gcc/ada/gnatlink.adb
@@ -1885,6 +1885,24 @@  begin
          Shared_Libgcc_Seen : Boolean := False;
          Static_Libgcc_Seen : Boolean := False;
 
+         function Is_Prefix
+           (Complete_String : String; Prefix : String) return Boolean;
+         --  Returns whether Prefix is a prefix of Complete_String
+
+         ---------------
+         -- Is_Prefix --
+         ---------------
+
+         function Is_Prefix
+           (Complete_String : String; Prefix : String) return Boolean
+         is
+            S : String renames Complete_String;
+            P : String renames Prefix;
+         begin
+            return P'Length <= S'Length
+              and then S (S'First .. S'First + P'Length - 1) = P;
+         end Is_Prefix;
+
       begin
          J := Linker_Options.First;
          while J <= Linker_Options.Last loop
@@ -1936,13 +1954,12 @@  begin
             --  Here we just check for a canonical form that matches the
             --  pragma Linker_Options set in the NT runtime.
 
-            if (Linker_Options.Table (J)'Length > 17
-                and then Linker_Options.Table (J) (1 .. 17) =
-                  "-Xlinker --stack=")
-              or else
-                (Linker_Options.Table (J)'Length > 12
-                 and then Linker_Options.Table (J) (1 .. 12) =
-                       "-Wl,--stack=")
+            if Is_Prefix
+                 (Complete_String => Linker_Options.Table (J).all,
+                  Prefix => "-Xlinker --stack=")
+              or else Is_Prefix
+                        (Complete_String => Linker_Options.Table (J).all,
+                         Prefix => "-Wl,--stack=")
             then
                if Stack_Op then
                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=