瀏覽代碼

free allocations in clearenv

This aligns clearenv with the Linux man page by setting 'environ'
rather than '*environ' to NULL, and stops it from leaking entries
allocated by the libc.
Alexander Monakov 7 年之前
父節點
當前提交
cc0dbd5f09
共有 1 個文件被更改,包括 6 次插入2 次删除
  1. 6 2
      src/env/clearenv.c

+ 6 - 2
src/env/clearenv.c

@@ -1,10 +1,14 @@
 #define _GNU_SOURCE
 #include <stdlib.h>
+#include "libc.h"
 
-extern char **__environ;
+static void dummy(char *old, char *new) {}
+weak_alias(dummy, __env_rm_add);
 
 int clearenv()
 {
-	__environ[0] = 0;
+	char **e = __environ;
+	__environ = 0;
+	if (e) while (*e) __env_rm_add(*e++, 0);
 	return 0;
 }