From: Eric Botcazou <[email protected]>

The issue is that the finalization of an empty container does not deallocate
its subpool, thus creating a dangling reference to it from the global pool.

gcc/ada/ChangeLog:

        * libgnat/a-cbinho.adb (Clear): Deallocate the subpool even if the
        container is empty.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/libgnat/a-cbinho.adb | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/ada/libgnat/a-cbinho.adb b/gcc/ada/libgnat/a-cbinho.adb
index 5c5a8ed4139..43a9f8b8528 100644
--- a/gcc/ada/libgnat/a-cbinho.adb
+++ b/gcc/ada/libgnat/a-cbinho.adb
@@ -75,13 +75,13 @@ package body Ada.Containers.Bounded_Indefinite_Holders is
 
    procedure Clear (Container : in out Holder) is
    begin
-      if Is_Empty (Container) then
-         return; -- nothing to do
+      if not Is_Empty (Container) then
+         if Container.Busy /= 0 then
+            raise Program_Error with "attempt to tamper with elements";
+         end if;
+         Free (Container.Element); -- finalize element
       end if;
-      if Container.Busy /= 0 then
-         raise Program_Error with "attempt to tamper with elements";
-      end if;
-      Free (Container.Element); -- finalize element
+      --  Unconditionally deallocate the subpool for Finalize, see below
       Ada.Unchecked_Deallocate_Subpool (Container.Handle);
    end Clear;
 
-- 
2.53.0

Reply via email to