]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix 0-sized secondary stack allocations
authorMarc Poulhiès <poulhies@adacore.com>
Thu, 30 Jun 2022 07:50:02 +0000 (09:50 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Tue, 12 Jul 2022 12:24:15 +0000 (12:24 +0000)
The Has_Enough_Free_Memory was not correctly reporting a completely full
chunk in the case of a 0-sized allocation.

gcc/ada/

* libgnat/s-secsta.adb (Has_Enough_Free_Memory): Check for full
chunk before computing the available size.

gcc/ada/libgnat/s-secsta.adb

index 24b7601c4b50cc9fbcc0661670bb677936c2b328..359e940333940f8344528257c6e7b84d7b981282 100644 (file)
@@ -506,12 +506,17 @@ package body System.Secondary_Stack is
       Mem_Size : Memory_Size) return Boolean
    is
    begin
+      --  First check if the chunk is full (Byte is > Memory'Last in that
+      --  case), then check there is enough free memory.
+
       --  Byte - 1 denotes the last occupied byte. Subtracting that byte from
       --  the memory capacity of the chunk yields the size of the free memory
       --  within the chunk. The chunk can fit the request as long as the free
       --  memory is as big as the request.
 
-      return Chunk.Size - (Byte - 1) >= Mem_Size;
+      return Chunk.Memory'Last >= Byte
+        and then Chunk.Size - (Byte - 1) >= Mem_Size;
+
    end Has_Enough_Free_Memory;
 
    ----------------------