]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Reference in Unbounded_String is almost never null
authorPiotr Trojanek <trojanek@adacore.com>
Wed, 27 Oct 2021 08:33:32 +0000 (10:33 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Tue, 9 Nov 2021 09:44:48 +0000 (09:44 +0000)
gcc/ada/

* libgnat/a-strunb.adb (Deallocate): Rename Reference_Copy to
Old, to make the code similar to other routines in this package.
(Realloc_For_Chunk): Use a temporary, deallocate the previous
string using a null-allowing copy of the string reference.

gcc/ada/libgnat/a-strunb.adb

index 0d62e4b379f0a18a133e927940529b3cdafd8255..b0e455b9edb30bd91b738ce63d1d0cb28ec58e2d 100644 (file)
@@ -506,11 +506,11 @@ package body Ada.Strings.Unbounded is
 
       if Object.Reference /= Null_String'Access then
          declare
-            Reference_Copy : String_Access := Object.Reference;
+            Old : String_Access := Object.Reference;
             --  The original reference cannot be null, so we must create a
             --  copy which will become null when deallocated.
          begin
-            Deallocate (Reference_Copy);
+            Deallocate (Old);
             Object.Reference := Null_Unbounded_String.Reference;
          end;
          Object.Last := 0;
@@ -833,9 +833,13 @@ package body Ada.Strings.Unbounded is
             Tmp : constant String_Access :=
               new String (1 .. New_Rounded_Up_Size);
 
+            Old : String_Access := Source.Reference;
+            --  The original reference cannot be null, so we must create a copy
+            --  which will become null when deallocated.
+
          begin
             Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
-            Free (Source.Reference);
+            Free (Old);
             Source.Reference := Tmp;
          end;
       end if;