Skip to content

Commit aa39233

Browse files
committed
Fix GH-10611: fpm_env_init_main leaks environ
1 parent d9ac59b commit aa39233

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sapi/fpm/fpm/fpm_env.c

Lines changed: 23 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,24 @@ 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+
environ = NULL;
206+
unsigned int i = 0;
207+
while (allocated_environ[i]) {
208+
free(allocated_environ[i]);
209+
i++;
210+
}
211+
free(allocated_environ);
212+
}
213+
#endif
214+
#endif
215+
197216
int fpm_env_init_main(void)
198217
{
199218
struct fpm_worker_pool_s *wp;
@@ -254,6 +273,10 @@ int fpm_env_init_main(void)
254273
env_nb++;
255274
}
256275

276+
if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT_EXIT_MAIN, fpm_env_cleanup, 0)) {
277+
return -1;
278+
}
279+
257280
if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
258281
return -1;
259282
}

0 commit comments

Comments
 (0)