Skip to content

Fix GH-10611: fpm_env_init_main leaks environ #10618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions sapi/fpm/fpm/fpm_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "fpm_env.h"
#include "fpm.h"
#include "fpm_cleanup.h"

#ifndef HAVE_SETPROCTITLE
#if defined(__linux__) || defined(__APPLE__)
Expand Down Expand Up @@ -194,6 +195,26 @@ static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
}
/* }}} */


#ifndef HAVE_SETPROCTITLE
#if defined(__linux__) || defined(__APPLE__)
/* Frees our copied environment variables. */
static void fpm_env_cleanup(int which, void *arg) /* {{{ */
{
char** allocated_environ = environ;
if (allocated_environ) {
environ = NULL;
unsigned int i = 0;
while (allocated_environ[i]) {
free(allocated_environ[i]);
i++;
}
free(allocated_environ);
}
}
#endif
#endif

int fpm_env_init_main(void)
{
struct fpm_worker_pool_s *wp;
Expand Down Expand Up @@ -254,6 +275,10 @@ int fpm_env_init_main(void)
env_nb++;
}

if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT_EXIT_MAIN, fpm_env_cleanup, 0)) {
return -1;
}

if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
return -1;
}
Expand Down