public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Slice length computation bug in Generic_Bounded_Length generics
@ 2022-09-06  7:15 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2022-09-06  7:15 UTC (permalink / raw)
  To: gcc-patches; +Cc: Steve Baird

[-- Attachment #1: Type: text/plain, Size: 473 bytes --]

In some cases involving null slices, the Slice subprograms (both the
  function and the procedure) in each of the three Generic_Bounded_Length
  generic packages (for String, Wide_String, and Wide_Wide_String)
  could raise Constraint_Error in cases where this is incorrect.

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

gcc/ada/

	* libgnat/a-strsup.adb, libgnat/a-stwisu.adb, libgnat/a-stzsup.adb
	(Super_Slice function and procedure): fix slice length computation.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 3482 bytes --]

diff --git a/gcc/ada/libgnat/a-strsup.adb b/gcc/ada/libgnat/a-strsup.adb
--- a/gcc/ada/libgnat/a-strsup.adb
+++ b/gcc/ada/libgnat/a-strsup.adb
@@ -1651,10 +1651,9 @@ package body Ada.Strings.Superbounded with SPARK_Mode is
             raise Index_Error;
          end if;
 
-         if High >= Low then
-            Result.Data (1 .. High - Low + 1) := Source.Data (Low .. High);
-            Result.Current_Length := High - Low + 1;
-         end if;
+         Result.Current_Length := (if Low > High then 0 else High - Low + 1);
+         Result.Data (1 .. Result.Current_Length) :=
+           Source.Data (Low .. High);
       end return;
    end Super_Slice;
 
@@ -1671,12 +1670,8 @@ package body Ada.Strings.Superbounded with SPARK_Mode is
          raise Index_Error;
       end if;
 
-      if High >= Low then
-         Target.Data (1 .. High - Low + 1) := Source.Data (Low .. High);
-         Target.Current_Length := High - Low + 1;
-      else
-         Target.Current_Length := 0;
-      end if;
+      Target.Current_Length := (if Low > High then 0 else High - Low + 1);
+      Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High);
    end Super_Slice;
 
    ----------------


diff --git a/gcc/ada/libgnat/a-stwisu.adb b/gcc/ada/libgnat/a-stwisu.adb
--- a/gcc/ada/libgnat/a-stwisu.adb
+++ b/gcc/ada/libgnat/a-stwisu.adb
@@ -1497,7 +1497,7 @@ package body Ada.Strings.Wide_Superbounded is
             raise Index_Error;
          end if;
 
-         Result.Current_Length := High - Low + 1;
+         Result.Current_Length := (if Low > High then 0 else High - Low + 1);
          Result.Data (1 .. Result.Current_Length) := Source.Data (Low .. High);
       end return;
    end Super_Slice;
@@ -1513,10 +1513,10 @@ package body Ada.Strings.Wide_Superbounded is
         or else High > Source.Current_Length
       then
          raise Index_Error;
-      else
-         Target.Current_Length := High - Low + 1;
-         Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High);
       end if;
+
+      Target.Current_Length := (if Low > High then 0 else High - Low + 1);
+      Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High);
    end Super_Slice;
 
    ----------------


diff --git a/gcc/ada/libgnat/a-stzsup.adb b/gcc/ada/libgnat/a-stzsup.adb
--- a/gcc/ada/libgnat/a-stzsup.adb
+++ b/gcc/ada/libgnat/a-stzsup.adb
@@ -1498,11 +1498,11 @@ package body Ada.Strings.Wide_Wide_Superbounded is
            or else High > Source.Current_Length
          then
             raise Index_Error;
-         else
-            Result.Current_Length := High - Low + 1;
-            Result.Data (1 .. Result.Current_Length) :=
-              Source.Data (Low .. High);
          end if;
+
+         Result.Current_Length := (if Low > High then 0 else High - Low + 1);
+         Result.Data (1 .. Result.Current_Length) :=
+           Source.Data (Low .. High);
       end return;
    end Super_Slice;
 
@@ -1517,10 +1517,10 @@ package body Ada.Strings.Wide_Wide_Superbounded is
         or else High > Source.Current_Length
       then
          raise Index_Error;
-      else
-         Target.Current_Length := High - Low + 1;
-         Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High);
       end if;
+
+      Target.Current_Length := (if Low > High then 0 else High - Low + 1);
+      Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High);
    end Super_Slice;
 
    ----------------



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-09-06  7:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-06  7:15 [Ada] Slice length computation bug in Generic_Bounded_Length generics Marc Poulhiès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).