diff mbox series

[Ada] Fix buffer overrun for small string concatenation at -O0

Message ID 20220712122512.GA3404631@adacore.com
State New
Headers show
Series [Ada] Fix buffer overrun for small string concatenation at -O0 | expand

Commit Message

Pierre-Marie de Rodat July 12, 2022, 12:25 p.m. UTC
The concatenation routines may read too much data on the source side when
the destination buffer is larger than the final result.  This change makes
sure that this does not happen any more and also removes obsolete stuff.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* rtsfind.ads (RE_Id): Remove RE_Str_Concat_Bounds_N values.
	(RE_Unit_Table): Remove RE_Str_Concat_Bounds_N entries.
	* libgnat/s-conca2.ads (Str_Concat_2): Adjust head comment.
	(Str_Concat_Bounds_2): Delete.
	* libgnat/s-conca2.adb (Str_Concat_2): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_2): Delete.
	* libgnat/s-conca3.ads (Str_Concat_3): Adjust head comment.
	(Str_Concat_Bounds_3): Delete.
	* libgnat/s-conca3.adb (Str_Concat_3): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_3): Delete.
	* libgnat/s-conca4.ads (Str_Concat_4): Adjust head comment.
	(Str_Concat_Bounds_4): Delete.
	* libgnat/s-conca4.adb (Str_Concat_4): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_4): Delete.
	* libgnat/s-conca5.ads (Str_Concat_5): Adjust head comment.
	(Str_Concat_Bounds_5): Delete.
	* libgnat/s-conca5.adb (Str_Concat_5): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_5): Delete.
	* libgnat/s-conca6.ads (Str_Concat_6): Adjust head comment.
	(Str_Concat_Bounds_6): Delete.
	* libgnat/s-conca6.adb (Str_Concat_6): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_6): Delete.
	* libgnat/s-conca7.ads (Str_Concat_7): Adjust head comment.
	(Str_Concat_Bounds_7): Delete.
	* libgnat/s-conca7.adb (Str_Concat_7): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_7): Delete.
	* libgnat/s-conca8.ads (Str_Concat_8): Adjust head comment.
	(Str_Concat_Bounds_8): Delete.
	* libgnat/s-conca8.adb (Str_Concat_8): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_8): Delete.
	* libgnat/s-conca9.ads (Str_Concat_9): Adjust head comment.
	(Str_Concat_Bounds_9): Delete.
	* libgnat/s-conca9.adb (Str_Concat_9): Use the length of the last
	input to size the last assignment.
	(Str_Concat_Bounds_9): Delete.
diff mbox series

Patch

diff --git a/gcc/ada/libgnat/s-conca2.adb b/gcc/ada/libgnat/s-conca2.adb
--- a/gcc/ada/libgnat/s-conca2.adb
+++ b/gcc/ada/libgnat/s-conca2.adb
@@ -46,26 +46,8 @@  package body System.Concat_2 is
       R (F .. L) := S1;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S2'Length - 1;
       R (F .. L) := S2;
    end Str_Concat_2;
 
-   -------------------------
-   -- Str_Concat_Bounds_2 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_2
-     (Lo, Hi : out Natural;
-      S1, S2 : String)
-   is
-   begin
-      if S1 = "" then
-         Lo := S2'First;
-         Hi := S2'Last;
-      else
-         Lo := S1'First;
-         Hi := S1'Last + S2'Length;
-      end if;
-   end Str_Concat_Bounds_2;
-
 end System.Concat_2;


diff --git a/gcc/ada/libgnat/s-conca2.ads b/gcc/ada/libgnat/s-conca2.ads
--- a/gcc/ada/libgnat/s-conca2.ads
+++ b/gcc/ada/libgnat/s-conca2.ads
@@ -36,15 +36,8 @@  package System.Concat_2 is
 
    procedure Str_Concat_2 (R : out String; S1, S2 : String);
    --  Performs the operation R := S1 & S2. The bounds of R are known to be
-   --  correct (usually set by a call to the Str_Concat_Bounds_2 procedure
-   --  below), so no bounds checks are required, and it is known that none of
+   --  sufficient so no bound checks are required, and it is known that none of
    --  the input operands overlaps R. No assumptions can be made about the
    --  lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_2
-     (Lo, Hi : out Natural;
-      S1, S2 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the two
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_2;


diff --git a/gcc/ada/libgnat/s-conca3.adb b/gcc/ada/libgnat/s-conca3.adb
--- a/gcc/ada/libgnat/s-conca3.adb
+++ b/gcc/ada/libgnat/s-conca3.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_2;
-
 package body System.Concat_3 is
 
    pragma Suppress (All_Checks);
@@ -52,25 +50,8 @@  package body System.Concat_3 is
       R (F .. L) := S2;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S3'Length - 1;
       R (F .. L) := S3;
    end Str_Concat_3;
 
-   -------------------------
-   -- Str_Concat_Bounds_3 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_3
-     (Lo, Hi     : out Natural;
-      S1, S2, S3 : String)
-   is
-   begin
-      System.Concat_2.Str_Concat_Bounds_2 (Lo, Hi, S2, S3);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_3;
-
 end System.Concat_3;


diff --git a/gcc/ada/libgnat/s-conca3.ads b/gcc/ada/libgnat/s-conca3.ads
--- a/gcc/ada/libgnat/s-conca3.ads
+++ b/gcc/ada/libgnat/s-conca3.ads
@@ -36,15 +36,8 @@  package System.Concat_3 is
 
    procedure Str_Concat_3 (R : out String; S1, S2, S3 : String);
    --  Performs the operation R := S1 & S2 & S3. The bounds of R are known to
-   --  be correct (usually set by a call to the Str_Concat_Bounds_3 procedure
-   --  below), so no bounds checks are required, and it is known that none of
-   --  the input operands overlaps R. No assumptions can be made about the
+   --  be sufficient so no bound checks are required, and it is known that none
+   --  of the input operands overlaps R. No assumptions can be made about the
    --  lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_3
-     (Lo, Hi     : out Natural;
-      S1, S2, S3 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the three
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_3;


diff --git a/gcc/ada/libgnat/s-conca4.adb b/gcc/ada/libgnat/s-conca4.adb
--- a/gcc/ada/libgnat/s-conca4.adb
+++ b/gcc/ada/libgnat/s-conca4.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_3;
-
 package body System.Concat_4 is
 
    pragma Suppress (All_Checks);
@@ -56,25 +54,8 @@  package body System.Concat_4 is
       R (F .. L) := S3;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S4'Length - 1;
       R (F .. L) := S4;
    end Str_Concat_4;
 
-   -------------------------
-   -- Str_Concat_Bounds_4 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_4
-     (Lo, Hi         : out Natural;
-      S1, S2, S3, S4 : String)
-   is
-   begin
-      System.Concat_3.Str_Concat_Bounds_3 (Lo, Hi, S2, S3, S4);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_4;
-
 end System.Concat_4;


diff --git a/gcc/ada/libgnat/s-conca4.ads b/gcc/ada/libgnat/s-conca4.ads
--- a/gcc/ada/libgnat/s-conca4.ads
+++ b/gcc/ada/libgnat/s-conca4.ads
@@ -36,15 +36,8 @@  package System.Concat_4 is
 
    procedure Str_Concat_4 (R : out String; S1, S2, S3, S4 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4. The bounds
-   --  of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_5 procedure below), so no bounds checks are required,
+   --  of R are known to be sufficient so no bound checks are required,
    --  and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_4
-     (Lo, Hi         : out Natural;
-      S1, S2, S3, S4 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the four
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_4;


diff --git a/gcc/ada/libgnat/s-conca5.adb b/gcc/ada/libgnat/s-conca5.adb
--- a/gcc/ada/libgnat/s-conca5.adb
+++ b/gcc/ada/libgnat/s-conca5.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_4;
-
 package body System.Concat_5 is
 
    pragma Suppress (All_Checks);
@@ -60,25 +58,8 @@  package body System.Concat_5 is
       R (F .. L) := S4;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S5'Length - 1;
       R (F .. L) := S5;
    end Str_Concat_5;
 
-   -------------------------
-   -- Str_Concat_Bounds_5 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_5
-     (Lo, Hi             : out Natural;
-      S1, S2, S3, S4, S5 : String)
-   is
-   begin
-      System.Concat_4.Str_Concat_Bounds_4 (Lo, Hi, S2, S3, S4, S5);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_5;
-
 end System.Concat_5;


diff --git a/gcc/ada/libgnat/s-conca5.ads b/gcc/ada/libgnat/s-conca5.ads
--- a/gcc/ada/libgnat/s-conca5.ads
+++ b/gcc/ada/libgnat/s-conca5.ads
@@ -36,15 +36,8 @@  package System.Concat_5 is
 
    procedure Str_Concat_5 (R : out String; S1, S2, S3, S4, S5 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4 & S5. The bounds
-   --  of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_5 procedure below), so no bounds checks are required,
+   --  of R are known to be sufficient so no bound checks are required,
    --  and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_5
-     (Lo, Hi             : out Natural;
-      S1, S2, S3, S4, S5 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the five
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_5;


diff --git a/gcc/ada/libgnat/s-conca6.adb b/gcc/ada/libgnat/s-conca6.adb
--- a/gcc/ada/libgnat/s-conca6.adb
+++ b/gcc/ada/libgnat/s-conca6.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_5;
-
 package body System.Concat_6 is
 
    pragma Suppress (All_Checks);
@@ -64,25 +62,8 @@  package body System.Concat_6 is
       R (F .. L) := S5;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S6'Length - 1;
       R (F .. L) := S6;
    end Str_Concat_6;
 
-   -------------------------
-   -- Str_Concat_Bounds_6 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_6
-     (Lo, Hi                 : out Natural;
-      S1, S2, S3, S4, S5, S6 : String)
-   is
-   begin
-      System.Concat_5.Str_Concat_Bounds_5 (Lo, Hi, S2, S3, S4, S5, S6);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_6;
-
 end System.Concat_6;


diff --git a/gcc/ada/libgnat/s-conca6.ads b/gcc/ada/libgnat/s-conca6.ads
--- a/gcc/ada/libgnat/s-conca6.ads
+++ b/gcc/ada/libgnat/s-conca6.ads
@@ -36,15 +36,8 @@  package System.Concat_6 is
 
    procedure Str_Concat_6 (R : out String; S1, S2, S3, S4, S5, S6 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4 & S5 & S6. The
-   --  bounds of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_6 procedure below), so no bounds checks are required,
+   --  bounds of R are known to be sufficient so no bound checks are required,
    --  and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_6
-     (Lo, Hi                 : out Natural;
-      S1, S2, S3, S4, S5, S6 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the six
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_6;


diff --git a/gcc/ada/libgnat/s-conca7.adb b/gcc/ada/libgnat/s-conca7.adb
--- a/gcc/ada/libgnat/s-conca7.adb
+++ b/gcc/ada/libgnat/s-conca7.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_6;
-
 package body System.Concat_7 is
 
    pragma Suppress (All_Checks);
@@ -71,25 +69,8 @@  package body System.Concat_7 is
       R (F .. L) := S6;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S7'Length - 1;
       R (F .. L) := S7;
    end Str_Concat_7;
 
-   -------------------------
-   -- Str_Concat_Bounds_7 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_7
-     (Lo, Hi                     : out Natural;
-      S1, S2, S3, S4, S5, S6, S7 : String)
-   is
-   begin
-      System.Concat_6.Str_Concat_Bounds_6 (Lo, Hi, S2, S3, S4, S5, S6, S7);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_7;
-
 end System.Concat_7;


diff --git a/gcc/ada/libgnat/s-conca7.ads b/gcc/ada/libgnat/s-conca7.ads
--- a/gcc/ada/libgnat/s-conca7.ads
+++ b/gcc/ada/libgnat/s-conca7.ads
@@ -38,15 +38,8 @@  package System.Concat_7 is
      (R                          : out String;
       S1, S2, S3, S4, S5, S6, S7 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4 & S5 & S6 & S7. The
-   --  bounds of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_8 procedure below), so no bounds checks are required,
+   --  bounds of R are known to be sufficient so no bound checks are required,
    --  and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_7
-     (Lo, Hi                     : out Natural;
-      S1, S2, S3, S4, S5, S6, S7 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the seven
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_7;


diff --git a/gcc/ada/libgnat/s-conca8.adb b/gcc/ada/libgnat/s-conca8.adb
--- a/gcc/ada/libgnat/s-conca8.adb
+++ b/gcc/ada/libgnat/s-conca8.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_7;
-
 package body System.Concat_8 is
 
    pragma Suppress (All_Checks);
@@ -75,26 +73,8 @@  package body System.Concat_8 is
       R (F .. L) := S7;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S8'Length - 1;
       R (F .. L) := S8;
    end Str_Concat_8;
 
-   -------------------------
-   -- Str_Concat_Bounds_8 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_8
-     (Lo, Hi                         : out Natural;
-      S1, S2, S3, S4, S5, S6, S7, S8 : String)
-   is
-   begin
-      System.Concat_7.Str_Concat_Bounds_7
-        (Lo, Hi, S2, S3, S4, S5, S6, S7, S8);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_8;
-
 end System.Concat_8;


diff --git a/gcc/ada/libgnat/s-conca8.ads b/gcc/ada/libgnat/s-conca8.ads
--- a/gcc/ada/libgnat/s-conca8.ads
+++ b/gcc/ada/libgnat/s-conca8.ads
@@ -38,15 +38,8 @@  package System.Concat_8 is
      (R                              : out String;
       S1, S2, S3, S4, S5, S6, S7, S8 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8.
-   --  The bounds of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_8 procedure below), so no bounds checks are required,
-   --  and it is known that none of the input operands overlaps R. No
+   --  The bounds of R are known to be sufficient so no bound checks are
+   --  required and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_8
-     (Lo, Hi                         : out Natural;
-      S1, S2, S3, S4, S5, S6, S7, S8 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the eight
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_8;


diff --git a/gcc/ada/libgnat/s-conca9.adb b/gcc/ada/libgnat/s-conca9.adb
--- a/gcc/ada/libgnat/s-conca9.adb
+++ b/gcc/ada/libgnat/s-conca9.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with System.Concat_8;
-
 package body System.Concat_9 is
 
    pragma Suppress (All_Checks);
@@ -79,26 +77,8 @@  package body System.Concat_9 is
       R (F .. L) := S8;
 
       F := L + 1;
-      L := R'Last;
+      L := F + S9'Length - 1;
       R (F .. L) := S9;
    end Str_Concat_9;
 
-   -------------------------
-   -- Str_Concat_Bounds_9 --
-   -------------------------
-
-   procedure Str_Concat_Bounds_9
-     (Lo, Hi                             : out Natural;
-      S1, S2, S3, S4, S5, S6, S7, S8, S9 : String)
-   is
-   begin
-      System.Concat_8.Str_Concat_Bounds_8
-        (Lo, Hi, S2, S3, S4, S5, S6, S7, S8, S9);
-
-      if S1 /= "" then
-         Hi := S1'Last + Hi - Lo + 1;
-         Lo := S1'First;
-      end if;
-   end Str_Concat_Bounds_9;
-
 end System.Concat_9;


diff --git a/gcc/ada/libgnat/s-conca9.ads b/gcc/ada/libgnat/s-conca9.ads
--- a/gcc/ada/libgnat/s-conca9.ads
+++ b/gcc/ada/libgnat/s-conca9.ads
@@ -38,15 +38,8 @@  package System.Concat_9 is
      (R                                  : out String;
       S1, S2, S3, S4, S5, S6, S7, S8, S9 : String);
    --  Performs the operation R := S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8 & S9.
-   --  The bounds of R are known to be correct (usually set by a call to the
-   --  Str_Concat_Bounds_9 procedure below), so no bounds checks are required,
-   --  and it is known that none of the input operands overlaps R. No
+   --  The bounds of R are known to be sufficient so no bound checks are
+   --  required, and it is known that none of the input operands overlaps R. No
    --  assumptions can be made about the lower bounds of any of the operands.
 
-   procedure Str_Concat_Bounds_9
-     (Lo, Hi                             : out Natural;
-      S1, S2, S3, S4, S5, S6, S7, S8, S9 : String);
-   --  Assigns to Lo..Hi the bounds of the result of concatenating the nine
-   --  given strings, following the rules in the RM regarding null operands.
-
 end System.Concat_9;


diff --git a/gcc/ada/rtsfind.ads b/gcc/ada/rtsfind.ads
--- a/gcc/ada/rtsfind.ads
+++ b/gcc/ada/rtsfind.ads
@@ -910,15 +910,6 @@  package Rtsfind is
      RE_Str_Concat_8,                    -- System.Concat_8
      RE_Str_Concat_9,                    -- System.Concat_9
 
-     RE_Str_Concat_Bounds_2,             -- System.Concat_2
-     RE_Str_Concat_Bounds_3,             -- System.Concat_3
-     RE_Str_Concat_Bounds_4,             -- System.Concat_4
-     RE_Str_Concat_Bounds_5,             -- System.Concat_5
-     RE_Str_Concat_Bounds_6,             -- System.Concat_6
-     RE_Str_Concat_Bounds_7,             -- System.Concat_7
-     RE_Str_Concat_Bounds_8,             -- System.Concat_8
-     RE_Str_Concat_Bounds_9,             -- System.Concat_9
-
      RE_Get_Active_Partition_Id,         -- System.DSA_Services
      RE_Get_Local_Partition_Id,          -- System.DSA_Services
      RE_Get_Passive_Partition_Id,        -- System.DSA_Services
@@ -2608,15 +2599,6 @@  package Rtsfind is
      RE_Str_Concat_8                     => System_Concat_8,
      RE_Str_Concat_9                     => System_Concat_9,
 
-     RE_Str_Concat_Bounds_2              => System_Concat_2,
-     RE_Str_Concat_Bounds_3              => System_Concat_3,
-     RE_Str_Concat_Bounds_4              => System_Concat_4,
-     RE_Str_Concat_Bounds_5              => System_Concat_5,
-     RE_Str_Concat_Bounds_6              => System_Concat_6,
-     RE_Str_Concat_Bounds_7              => System_Concat_7,
-     RE_Str_Concat_Bounds_8              => System_Concat_8,
-     RE_Str_Concat_Bounds_9              => System_Concat_9,
-
      RE_Get_Active_Partition_Id          => System_DSA_Services,
      RE_Get_Local_Partition_Id           => System_DSA_Services,
      RE_Get_Passive_Partition_Id         => System_DSA_Services,