Re: [PATCH] libgfortran: Fix up putenv uses in libcaf_shmem [PR124330]
Jerry D <[email protected]>
| Newsgroups | gmane.comp.gcc.patches,gmane.comp.gcc.fortran |
|---|---|
| Message-ID | <[email protected]> |
On 3/4/26 7:44 AM, John David Anglin wrote: > Hi Jakub, > > I believe this change is obvious. The Linux manpage for putenv states: > > In particular, this string > becomes part of the environment; changing it later will change the envi‐ > ronment. (Thus, it is an error to call putenv() with an automatic vari‐ > able as the argument, then return from the calling function while string > is still part of the environment.) > > Some systems may have used a copy but this causes a memory leak. On HP-UX, altering > the string changes the environment, so I believe it doesn't use a copy. So, we want > a static string. > > Thanks, > Dave Yes, agree, OK for trunk. Jerry > > On 2026-03-04 10:12 a.m., Jakub Jelinek wrote: >> Hi! >> >> I don't have access to HP/UX, but at least on other OSes and what Linux as >> well as POSIX documents is that when you call putenv with some argument, >> what that argument points to becomes part of the environment and when >> it is changed, the environment changes. I believe ENOMEM from putenv is >> about reallocating of the __environ (or similar) pointed array of pointers >> (e.g. if the particular env var name isn't there already), it still >> shouldn't allocate any memory for the NAME=VALUE string and just use >> the user provided. So, padding address of automatic array will be UB >> as soon as the scope of that var is left. >> >> One can either malloc the buffer, or use static vars, then nothing leaks >> and in the unlikely case putenv would be called twice for the same env var, >> it would second time only register the same buffer. >> >> Ok for trunk? >> >> 2026-03-04 Jakub Jelinek <[email protected]> >> >> PR libfortran/124330 >> * caf/shmem/shared_memory.c (shared_memory_set_env): Make buffer >> used by putenv static. >> (shared_memory_init): Likewise. >> >> --- libgfortran/caf/shmem/shared_memory.c.jj 2026-03-04 09:15:13.572332552 +0100 >> +++ libgfortran/caf/shmem/shared_memory.c 2026-03-04 14:01:14.511957983 +0100 >> @@ -71,7 +71,7 @@ shared_memory_set_env (pid_t pid) >> snprintf (val, 20, "%d", pid); >> SetEnvironmentVariable (ENV_PPID, val); >> #else >> - char buffer[28]; >> + static char buffer[28]; >> int res; >> >> /* HP-UX / Legacy Fallback using putenv */ >> @@ -253,7 +253,7 @@ shared_memory_init (shared_memory_act *m >> snprintf (val, 20, "%p", mem->glbl.base); >> SetEnvironmentVariable (ENV_BASE, val); >> #else >> - char buffer[28]; >> + static char buffer[28]; >> int res; >> >> /* HP-UX / Legacy Fallback using putenv */ >> >> Jakub >> > >