public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-4674] [Ada] Reference in Unbounded_String is almost never null
@ 2021-10-25 15:08 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2021-10-25 15:08 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:98f939e9c98403636f10168be568bb9b800aac41

commit r12-4674-g98f939e9c98403636f10168be568bb9b800aac41
Author: Piotr Trojanek <trojanek@adacore.com>
Date:   Tue Jun 15 23:32:51 2021 +0200

    [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:
---
 gcc/ada/libgnat/a-strunb.adb | 10 ++++++++--
 gcc/ada/libgnat/a-strunb.ads |  4 ++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/gcc/ada/libgnat/a-strunb.adb b/gcc/ada/libgnat/a-strunb.adb
index 4727f965486..0d62e4b379f 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 b3050fdb5a3..2f5bd948f3b 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


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

only message in thread, other threads:[~2021-10-25 15:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 15:08 [gcc r12-4674] [Ada] Reference in Unbounded_String is almost never null Pierre-Marie de Rodat

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).