diff mbox series

[Ada] Reference in Unbounded_String is almost never null

Message ID 20211109094554.GA830178@adacore.com
State New
Headers show
Series [Ada] Reference in Unbounded_String is almost never null | expand

Commit Message

Pierre-Marie de Rodat Nov. 9, 2021, 9:45 a.m. UTC
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.
diff mbox series

Patch

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;