https://gcc.gnu.org/g:fc0cd2781a254046eaa1bc930ed2ab2050ac9dea

commit r17-1181-gfc0cd2781a254046eaa1bc930ed2ab2050ac9dea
Author: Mathias Aparicio <[email protected]>
Date:   Thu Mar 26 16:54:09 2026 +0100

    ada: Fix memory leak in __gnat_setenv
    
    Before this patch, the code executed on Linux used `putenv`, which
    requires a memory allocation. However, the memory was explicitly freed
    only for Windows (`__MINGW32__`) and older VxWorks targets. Because
    Linux fell into the allocation block but not the free block, there
    was a memory leak.
    
    To fix the leak, `__gnat_setenv` now calls `setenv` for Linux, doing
    the same thing we were already doing for macOS.
    
    Note that putting Linux in the Windows block is not a solution because,
    on Linux, `putenv` uses the pointer directly and does not copy the
    string. Therefore, if the memory were freed right away, there would be
    a use-after-free.
    
    To be careful, we do not use `setenv` everywhere else (except for old
    VxWorks targets and Windows), because there might be problems with other
    configurations if we do so.
    
    gcc/ada/ChangeLog:
    
            * env.c (__gnat_setenv): Add __linux__ preprocessor macro
            directive to the setenv block.

Diff:
---
 gcc/ada/env.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/env.c b/gcc/ada/env.c
index 2931220f069b..d37d2bc7a02d 100644
--- a/gcc/ada/env.c
+++ b/gcc/ada/env.c
@@ -100,10 +100,11 @@ void
 __gnat_setenv (char *name, char *value)
 {
 #if (defined (__vxworks) && (defined (__RTP__) || _WRS_VXWORKS_MAJOR >= 7)) \
-    || defined (__APPLE__)
+    || defined (__APPLE__) \
+    || defined (__linux__)
   setenv (name, value, 1);
 
-#else
+#else /* Why don't we use setenv on all platforms where it's available??? */
   size_t size = strlen (name) + strlen (value) + 2;
   char *expression;

Reply via email to