]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Reference in Unbounded_String is almost never null
authorPiotr Trojanek <trojanek@adacore.com>
Tue, 15 Jun 2021 21:32:51 +0000 (23:32 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 25 Oct 2021 15:07:20 +0000 (15:07 +0000)
gcc/ada/

* libgnat/a-strunb.ads (Unbounded_String): Reference is never
null.
* libgnat/a-strunb.adb (Finalize): Copy reference while it needs
to be deallocated.

gcc/ada/libgnat/a-strunb.adb
gcc/ada/libgnat/a-strunb.ads

index 4727f965486195356d9297eaf0bc13230b10366c..0d62e4b379f0a18a133e927940529b3cdafd8255 100644 (file)
@@ -505,8 +505,14 @@ package body Ada.Strings.Unbounded is
       --  Note: Don't try to free statically allocated null string
 
       if Object.Reference /= Null_String'Access then
-         Deallocate (Object.Reference);
-         Object.Reference := Null_Unbounded_String.Reference;
+         declare
+            Reference_Copy : 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);
+            Object.Reference := Null_Unbounded_String.Reference;
+         end;
          Object.Last := 0;
       end if;
    end Finalize;
index b3050fdb5a367121b9e51126b01f4a5fcf73bd7c..2f5bd948f3b1c54984a5d39a2b215a26d36464cd 100644 (file)
@@ -746,8 +746,8 @@ private
      renames To_Unbounded_String;
 
    type Unbounded_String is new AF.Controlled with record
-      Reference : String_Access := Null_String'Access;
-      Last      : Natural       := 0;
+      Reference : not null String_Access := Null_String'Access;
+      Last      : Natural                := 0;
    end record with Put_Image => Put_Image;
 
    procedure Put_Image