Skip to content

Commit 71c6372

Browse files
committed
Fix GH-10611: fpm_env_init_main leaks environ
Closes GH-10618.
1 parent 2c53d63 commit 71c6372

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PHP NEWS
1010
. Fixed bug GH-10801 (Named arguments in CTE functions cause a segfault).
1111
(nielsdos)
1212

13+
- FPM:
14+
. Fixed bug GH-10611 (fpm_env_init_main leaks environ). (nielsdos)
15+
1316
- FTP:
1417
. Propagate success status of ftp_close(). (nielsdos)
1518

sapi/fpm/fpm/fpm_env.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "fpm_env.h"
1313
#include "fpm.h"
14+
#include "fpm_cleanup.h"
1415

1516
#ifndef HAVE_SETPROCTITLE
1617
#if defined(__linux__) || defined(__APPLE__)
@@ -194,6 +195,26 @@ static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
194195
}
195196
/* }}} */
196197

198+
199+
#ifndef HAVE_SETPROCTITLE
200+
#if defined(__linux__) || defined(__APPLE__)
201+
/* Frees our copied environment variables. */
202+
static void fpm_env_cleanup(int which, void *arg) /* {{{ */
203+
{
204+
char** allocated_environ = environ;
205+
if (allocated_environ) {
206+
environ = NULL;
207+
unsigned int i = 0;
208+
while (allocated_environ[i]) {
209+
free(allocated_environ[i]);
210+
i++;
211+
}
212+
free(allocated_environ);
213+
}
214+
}
215+
#endif
216+
#endif
217+
197218
int fpm_env_init_main(void)
198219
{
199220
struct fpm_worker_pool_s *wp;
@@ -254,6 +275,10 @@ int fpm_env_init_main(void)
254275
env_nb++;
255276
}
256277

278+
if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT_EXIT_MAIN, fpm_env_cleanup, 0)) {
279+
return -1;
280+
}
281+
257282
if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
258283
return -1;
259284
}

0 commit comments

Comments
 (0)