From: Mathias Aparicio <[email protected]>

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.

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

---
 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 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;
 
-- 
2.53.0

Reply via email to