]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix memory leak in 'Image
authorBob Duff <duff@adacore.com>
Fri, 15 Dec 2023 21:59:43 +0000 (16:59 -0500)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 6 May 2024 09:11:26 +0000 (11:11 +0200)
Fix memory leak in 'Image by managing the secondary stack
in scopes that call the new Ada 2020 'Image, which calls 'Put_Image
and then Get, which returns on the secondary stack.

gcc/ada/

* exp_put_image.adb (Build_Image_Call): Call Set_Uses_Sec_Stack on
the current scope. We don't do this at all call sites, because
there are three; better to do it here.

gcc/ada/exp_put_image.adb

index c298163f36fdbe95692c5ec9bcb5b7d0eb54e94d..182497fb6e8d0e8e8281b2f5edb1eee5c5306716 100644 (file)
@@ -1290,6 +1290,14 @@ package body Exp_Put_Image is
          Actions := New_List (Sink_Decl, Put_Im, Result_Decl);
       end if;
 
+      --  To avoid leaks, we need to manage the secondary stack, because Get is
+      --  returning a String allocated thereon. It might be cleaner to let the
+      --  normal mechanisms for functions returning on the secondary stack call
+      --  Set_Uses_Sec_Stack, but this expansion of 'Image is happening too
+      --  late for that.
+
+      Set_Uses_Sec_Stack (Current_Scope);
+
       return Make_Expression_With_Actions (Loc,
         Actions    => Actions,
         Expression => New_Occurrence_Of (Result_Entity, Loc));