From: Mathias Aparicio Date: Thu, 26 Mar 2026 15:54:09 +0000 (+0100) Subject: ada: Fix memory leak in __gnat_setenv X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fc0cd2781a254046eaa1bc930ed2ab2050ac9dea;p=thirdparty%2Fgcc.git 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 --git a/gcc/ada/env.c b/gcc/ada/env.c index 2931220f069..d37d2bc7a02 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;