summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPiotr Trojanek <trojanek@adacore.com>2021-06-15 23:32:51 +0200
committerPierre-Marie de Rodat <derodat@adacore.com>2021-10-25 15:07:20 +0000
commit98f939e9c98403636f10168be568bb9b800aac41 (patch)
treefe0630350214e1d1f21bdaef6e0a3099f16cba6e
parentf977a792721cdaa0d6cbda689b3a6930cf510f7e (diff)
downloadgcc-98f939e9c98403636f10168be568bb9b800aac41.tar.gz
[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.
-rw-r--r--gcc/ada/libgnat/a-strunb.adb10
-rw-r--r--gcc/ada/libgnat/a-strunb.ads4
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