Skip to content
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

Free environ when RUBY_FREE_AT_EXIT #9463

Merged
merged 1 commit into from Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions common.mk
Expand Up @@ -19320,6 +19320,7 @@ vm.$(OBJEXT): $(top_srcdir)/internal/gc.h
vm.$(OBJEXT): $(top_srcdir)/internal/hash.h
vm.$(OBJEXT): $(top_srcdir)/internal/imemo.h
vm.$(OBJEXT): $(top_srcdir)/internal/inits.h
vm.$(OBJEXT): $(top_srcdir)/internal/missing.h
vm.$(OBJEXT): $(top_srcdir)/internal/numeric.h
vm.$(OBJEXT): $(top_srcdir)/internal/object.h
vm.$(OBJEXT): $(top_srcdir)/internal/parse.h
Expand Down
1 change: 1 addition & 0 deletions internal/missing.h
Expand Up @@ -13,6 +13,7 @@
/* missing/setproctitle.c */
#ifndef HAVE_SETPROCTITLE
extern void ruby_init_setproctitle(int argc, char *argv[]);
extern void ruby_free_proctitle(void);
#endif

#endif /* INTERNAL_MISSING_H */
42 changes: 38 additions & 4 deletions missing/setproctitle.c
Expand Up @@ -80,10 +80,20 @@ static char **argv1_addr = NULL;

#endif /* HAVE_SETPROCTITLE */

#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
# define ALLOCATE_ENVIRON 1
#else
# define ALLOCATE_ENVIRON 0
#endif

#if ALLOCATE_ENVIRON
static char **orig_environ = NULL;
#endif

void
compat_init_setproctitle(int argc, char *argv[])
{
#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
#if ALLOCATE_ENVIRON
extern char **environ;
char *lastargv = NULL;
char *lastenvp = NULL;
Expand All @@ -100,9 +110,10 @@ compat_init_setproctitle(int argc, char *argv[])
return;

/* Fail if we can't allocate room for the new environment */
for (i = 0; envp[i] != NULL; i++)
;
if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
for (i = 0; envp[i] != NULL; i++);

orig_environ = environ = xcalloc(i + 1, sizeof(*environ));
if (environ == NULL) {
environ = envp; /* put it back */
return;
}
Expand Down Expand Up @@ -134,6 +145,29 @@ compat_init_setproctitle(int argc, char *argv[])
#endif /* SPT_REUSEARGV */
}

void
ruby_free_proctitle(void)
{
#if ALLOCATE_ENVIRON
extern char **environ;
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved

if (!orig_environ) return; /* environ is allocated by OS */

for (int i = 0; environ[i] != NULL; i++) {
xfree(environ[i]);
}

/* ruby_setenv could allocate a new environ, so we need to free both environ
* orig_environ in that case. */
if (environ != orig_environ) {
xfree(orig_environ);
orig_environ = NULL;
}

xfree(environ);
#endif
}

#ifndef HAVE_SETPROCTITLE

void
Expand Down
5 changes: 5 additions & 0 deletions vm.c
Expand Up @@ -20,6 +20,7 @@
#include "internal/eval.h"
#include "internal/gc.h"
#include "internal/inits.h"
#include "internal/missing.h"
#include "internal/object.h"
#include "internal/proc.h"
#include "internal/re.h"
Expand Down Expand Up @@ -3034,6 +3035,10 @@ ruby_vm_destruct(rb_vm_t *vm)
xfree(th->nt);
th->nt = NULL;
}

#ifndef HAVE_SETPROCTITLE
ruby_free_proctitle();
#endif
}
else {
if (th) {
Expand Down