public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Reference in Unbounded_String is almost never null
@ 2021-10-25 15:09 Pierre-Marie de Rodat
  0 siblings, 0 replies; 2+ messages in thread
From: Pierre-Marie de Rodat @ 2021-10-25 15:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Piotr Trojanek

[-- Attachment #1: Type: text/plain, Size: 591 bytes --]

There are two variants of the Ada.Strings.Unbounded_String package, with
and without atomic reference counters. The underlying pointer is never
null in one variant (and had a null-excluding type) and almost never
null in the other variant (and now has a null-excluding type as well).

Cleanup related to sync of contracts for GNATprove between both variants
of the package.

Tested on x86_64-pc-linux-gnu, committed on trunk

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.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1401 bytes --]

diff --git a/gcc/ada/libgnat/a-strunb.adb b/gcc/ada/libgnat/a-strunb.adb
--- 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
--- 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] 2+ messages in thread

* [Ada] Reference in Unbounded_String is almost never null
@ 2021-11-09  9:45 Pierre-Marie de Rodat
  0 siblings, 0 replies; 2+ messages in thread
From: Pierre-Marie de Rodat @ 2021-11-09  9:45 UTC (permalink / raw)
  To: gcc-patches; +Cc: Piotr Trojanek

[-- Attachment #1: Type: text/plain, Size: 659 bytes --]

The underlying reference in Unbounded_String is almost never null, so
recently it was changed to a non-excluding type (to avoid runtime checks
that are almost never needed).

The low-level routines that modify that reference had to be adapted, but
only the Deallocate routine was adapted. This patch adapts the
Realloc_For_Chunk routine as well.

Tested on x86_64-pc-linux-gnu, committed on trunk

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.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1313 bytes --]

diff --git a/gcc/ada/libgnat/a-strunb.adb b/gcc/ada/libgnat/a-strunb.adb
--- a/gcc/ada/libgnat/a-strunb.adb
+++ b/gcc/ada/libgnat/a-strunb.adb
@@ -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;



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-09  9:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 15:09 [Ada] Reference in Unbounded_String is almost never null Pierre-Marie de Rodat
2021-11-09  9:45 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).