]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix buffer overrun for small string concatenation at -O0
authorEric Botcazou <ebotcazou@adacore.com>
Tue, 21 Jun 2022 20:17:13 +0000 (22:17 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Tue, 12 Jul 2022 12:24:11 +0000 (12:24 +0000)
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.

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.

17 files changed:
gcc/ada/libgnat/s-conca2.adb
gcc/ada/libgnat/s-conca2.ads
gcc/ada/libgnat/s-conca3.adb
gcc/ada/libgnat/s-conca3.ads
gcc/ada/libgnat/s-conca4.adb
gcc/ada/libgnat/s-conca4.ads
gcc/ada/libgnat/s-conca5.adb
gcc/ada/libgnat/s-conca5.ads
gcc/ada/libgnat/s-conca6.adb
gcc/ada/libgnat/s-conca6.ads
gcc/ada/libgnat/s-conca7.adb
gcc/ada/libgnat/s-conca7.ads
gcc/ada/libgnat/s-conca8.adb
gcc/ada/libgnat/s-conca8.ads
gcc/ada/libgnat/s-conca9.adb
gcc/ada/libgnat/s-conca9.ads
gcc/ada/rtsfind.ads

index 49982f5642aa23855da66f48514fbc3cf2079b14..2a263caa769d4463809da2e79e1f9a12a091e233 100644 (file)
@@ -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;
index f9c739333852af2991f19ba11582de79307f479d..450435a0cf246ae00ea5d18262a7d6bcb95e6fc0 100644 (file)
@@ -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;
index d607082ca75022dbe1e8c8fd68128817126d4d41..ddba8320ded55be0fa177f8368d77e964151db25 100644 (file)
@@ -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;
index d7282ff1a33772a08b35605c074fe7f74be8a77d..2ff3abc2ff0103d4d29258ea6a065bd43b880860 100644 (file)
@@ -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;
index 694033a42a0bf040e1de40e93d0a05447ca21699..e1c7e926e9d1a3569e6c7d150f4349685cfb55b4 100644 (file)
@@ -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;
index 88b464d57431237ec005fa0f0e858cb994dcbab2..ecc3108c977ca8c48d4e7a7bad8bd22205b85b25 100644 (file)
@@ -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;
index f611260c9557970b04e6a643a182120b8becae34..2283747d78a69a7a72fceedfb42120df28157106 100644 (file)
@@ -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;
index f6b89888fefb6b2062a4eadf045a8ecd2026001a..be7aaceda47dc2d26d28642f44290cb3bcb4c5ae 100644 (file)
@@ -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;
index 66b767f3daae399d29249ca6966ea5df36753f77..b574d0461461a68416ed309518abbdf0904f3a8e 100644 (file)
@@ -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;
index e753251acd9fdfafb1d8f00bb0b50ffdefe9814c..2aac3d0fd56292fe387e79b587446aeb7015f3d2 100644 (file)
@@ -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;
index 02508873ccc88e4eb0c4e2ae5a2eb568f4823b7e..e624b5cd9ac04a773b1f792c67fd2f5307c43eb8 100644 (file)
@@ -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;
index c130ddf5347578791108880e9011cbc1c00d070d..755499568e256d1bac332f35f6d9336221c8428a 100644 (file)
@@ -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;
index d6ee36cb1717d38b641c08019f50c3e394596633..98b2e59b65c14e54f66d66595f8c506f7f95d80a 100644 (file)
@@ -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;
index dda35c1e9749f89fb51e8917a79db20eef8deef1..a2491548548ba45920eec934b0755a4352dc6875 100644 (file)
@@ -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;
index bfe228e9d66cf015fa061c5fa0b482fcf2c8ee03..08860f505affde404f9213494c04f088a55d7f57 100644 (file)
@@ -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;
index 7737a1e20e6ceaaaac123a09776ed733b3ccb31a..39560ffd94d7f3276c7a4500ed1ae283efebe524 100644 (file)
@@ -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;
index 127095578efa1bd6b58e1e1bac2669fa4f3b73e7..65c64090371d409ffdcc6aca974069707dc04a3d 100644 (file)
@@ -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,