summaryrefslogtreecommitdiff
path: root/missing
diff options
context:
space:
mode:
authorPeter Zhu <[email protected]>2024-05-01 11:15:28 -0400
committerPeter Zhu <[email protected]>2024-05-02 10:12:58 -0400
commit4f69d318b8667eb10596aaec9b75992a3265f66d (patch)
tree14f22b6f59a3a01abcc033bcf7de191231928275 /missing
parent12cbfd8e2f0c2386803f835c3d8d55ac584e9e22 (diff)
Keep track of the originally allocated environ
We need to keep a pointer to the originally allocated environ because adding more environment variables can cause it to be changed to something else. For example: 100.times do |i| ENV["FOO#{i}"] = "1" end Causes Valgrind to report: 312 bytes in 1 blocks are definitely lost in loss record 9 of 13 at 0x484D444: calloc (vg_replace_malloc.c:1340) by 0x1884F8: calloc1 (gc.c:1844) by 0x1884F8: objspace_xcalloc (gc.c:12202) by 0x1884F8: ruby_xcalloc_body (gc.c:12209) by 0x4204DD: ruby_init_setproctitle (setproctitle.c:119) by 0x27DDF4: ruby_process_options (ruby.c:3101) by 0x160BD1: ruby_options (eval.c:117) by 0x15B96E: rb_main (main.c:40) by 0x15B96E: main (main.c:59)
Diffstat (limited to 'missing')
-rw-r--r--missing/setproctitle.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/missing/setproctitle.c b/missing/setproctitle.c
index 1522657b9b..72e214e51c 100644
--- a/missing/setproctitle.c
+++ b/missing/setproctitle.c
@@ -87,6 +87,7 @@ static char **argv1_addr = NULL;
#endif
#if ALLOCATE_ENVIRON
+static char **system_environ = NULL;
static char **orig_environ = NULL;
static char **alloc_environ = NULL;
#endif
@@ -113,10 +114,10 @@ compat_init_setproctitle(int argc, char *argv[])
/* Fail if we can't allocate room for the new environment */
for (i = 0; envp[i] != NULL; i++);
- orig_environ = environ;
+ system_environ = environ;
alloc_environ = xcalloc(i + 1, sizeof(*environ));
- environ = xcalloc(i + 1, sizeof(*environ));
+ orig_environ = environ = xcalloc(i + 1, sizeof(*environ));
if (environ == NULL) {
environ = envp; /* put it back */
return;
@@ -161,9 +162,9 @@ ruby_free_proctitle(void)
xfree(alloc_environ[i]);
}
xfree(alloc_environ);
- xfree(environ);
+ xfree(orig_environ);
- environ = orig_environ;
+ environ = system_environ;
#endif
}