From: Piotr Trojanek Date: Tue, 15 Jun 2021 21:32:51 +0000 (+0200) Subject: [Ada] Reference in Unbounded_String is almost never null X-Git-Tag: basepoints/gcc-13~3639 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98f939e9c98403636f10168be568bb9b800aac41;p=thirdparty%2Fgcc.git [Ada] Reference in Unbounded_String is almost never null 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. --- diff --git a/gcc/ada/libgnat/a-strunb.adb b/gcc/ada/libgnat/a-strunb.adb index 4727f9654861..0d62e4b379f0 100644 --- a/gcc/ada/libgnat/a-strunb.adb +++ b/gcc/ada/libgnat/a-strunb.adb @@ -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; diff --git a/gcc/ada/libgnat/a-strunb.ads b/gcc/ada/libgnat/a-strunb.ads index b3050fdb5a36..2f5bd948f3b1 100644 --- a/gcc/ada/libgnat/a-strunb.ads +++ b/gcc/ada/libgnat/a-strunb.ads @@ -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