]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix memory leak in __gnat_setenv
authorMathias Aparicio <aparicio@adacore.com>
Thu, 26 Mar 2026 15:54:09 +0000 (16:54 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 2 Jun 2026 08:42:24 +0000 (10:42 +0200)
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.

gcc/ada/env.c

index 2931220f069b431803e89f92124fb5d94ad08cff..d37d2bc7a02dff3816206c20261f10658b40c250 100644 (file)
@@ -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;