Loading...
stdlib/FreeBSD/setenv.c.patch Libc-498 Libc-763.11
--- Libc/Libc-498/stdlib/FreeBSD/setenv.c.patch
+++ Libc/Libc-763.11/stdlib/FreeBSD/setenv.c.patch
@@ -1,6 +1,6 @@
---- setenv.c.orig	2006-12-12 18:14:46.000000000 -0800
-+++ setenv.c	2006-12-12 18:22:12.000000000 -0800
-@@ -40,32 +40,60 @@
+--- setenv.c.orig	2011-04-13 01:21:14.000000000 -0700
++++ setenv.c	2011-04-13 14:44:04.000000000 -0700
+@@ -36,32 +36,115 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
  #include <stddef.h>
  #include <stdlib.h>
  #include <string.h>
@@ -12,26 +12,81 @@
  
 -char *__findenv(const char *, int *);
 +#define	ZONE_OWNS_PTR(zone, ptr)	(malloc_zone_from_ptr((ptr)) == zone)
- 
++
 +extern malloc_zone_t *__zone0;
 +extern void __malloc_check_env_name(const char *);
 +
 +__private_extern__ char *__findenv(const char *, int *, char **);
 +__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *);
 +__private_extern__ void __unsetenv(const char *, char **, malloc_zone_t *);
-+
-+#ifndef BUILDING_VARIANT
++__private_extern__ int init__zone0(int);
+ 
  /*
 - * setenv --
 - *	Set the value of the environmental variable "name" to be
 - *	"value".  If rewrite is set, replace any current value.
++ * _cthread_init_routine used to be called from crt1.o to initialize threads.
++ * This is no longer needed, as initialization happens in dylib initializers,
++ * but is provided to maintain backwards compatibility.  Normally, for 10.5
++ * or greater, _cthread_init_routine does nothing.
++ *
++ * Before 10.5, the _start routine in crt1.o clobbers environ with the original
++ * stack value, which effectively undoes any environment changes made in
++ * initializers.  When LEGACY_CRT1_ENVIRON is defined, we replace the
++ * do-nothing routine with one that attempts to restore the environ value.
++ * But this only works if the setenv (and family) routines were used
++ * exclusively, (no direct manipulation of environ).  Note that according to
++ * SUSv3, direct manipulation of environ may result in undefined behavior in
++ * setenv and family, so we don't support that (on less than 10.5).
+  */
+-int
+-setenv(name, value, rewrite)
++#ifdef BUILDING_VARIANT
++#  ifdef LEGACY_CRT1_ENVIRON
++extern char **_saved_environ;
++#  endif /* LEGACY_CRT1_ENVIRON */
++#else /* !BUILDING_VARIANT */
++#  ifdef LEGACY_CRT1_ENVIRON
++__private_extern__ char **_saved_environ = NULL;
++
++static int
++_legacy_crt1_environ(void)
++{
++	if (_saved_environ) *_NSGetEnviron() = _saved_environ;
++	return 0;
++}
++int (*_cthread_init_routine)(void) = _legacy_crt1_environ;
++
++#  else /* !LEGACY_CRT1_ENVIRON */
++static int _do_nothing(void) { return 0; }
++int (*_cthread_init_routine)(void) = _do_nothing;
++#  endif /* !LEGACY_CRT1_ENVIRON */
++
++/*
++ * Create the environment malloc zone and give it a recognizable name.
++ */
++__private_extern__ int
++init__zone0(int should_set_errno)
++{
++	if (__zone0) return (0);
++
++	__zone0 = malloc_create_zone(0, 0);
++	if (!__zone0) {
++		if (should_set_errno) {
++			errno = ENOMEM;
++		}
++		return (-1);
++	}
++	malloc_set_zone_name(__zone0, "environ");
++	return (0);
++}
++
++/*
 + * The copy flag may have 3 values:
 + *  1 - make a copy of the name/value pair
 + *  0 - take the name as a user-supplied name=value string
 + * -1 - like 0, except we copy of the name=value string in name
-  */
--int
--setenv(name, value, rewrite)
++ */
 +__private_extern__ int
 +__setenv(name, value, rewrite, copy, environp, envz)
  	const char *name;
@@ -76,7 +131,7 @@
  			while ( (*c++ = *value++) );
  			return (0);
  		}
-@@ -73,48 +101,250 @@
+@@ -69,48 +152,235 @@ setenv(name, value, rewrite)
  		int cnt;
  		char **p;
  
@@ -163,7 +218,12 @@
 +void *
 +_allocenvstate(void)
 +{
-+	return (void *)malloc_create_zone(1000 /* unused */, 0 /* unused */);
++	malloc_zone_t *zone;
++	zone = malloc_create_zone(1000 /* unused */, 0 /* unused */);
++	if (zone) {
++		malloc_set_zone_name(zone, "environ");
++	}
++	return (void *)zone;
 +}
 +
 +/*
@@ -219,14 +279,7 @@
 +int
 +_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
 +{
-+	/* insure __zone0 is set up */
-+	if (!__zone0) {
-+	    __zone0 = malloc_create_zone(0, 0);
-+	    if (!__zone0) {
-+		    errno = ENOMEM;
-+		    return (-1);
-+	    }
-+	}
++	if (init__zone0(1)) return (-1);
 +	return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
 +}
 +
@@ -239,14 +292,7 @@
 +int
 +_unsetenvp(const char *name, char ***envp, void *state)
 +{
-+	/* insure __zone0 is set up */
-+	if (!__zone0) {
-+	    __zone0 = malloc_create_zone(0, 0);
-+	    if (!__zone0) {
-+		    errno = ENOMEM;
-+		    return (-1);
-+	    }
-+	}
++	if (init__zone0(1)) return (-1);
 +	__unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
 +	return 0;
 +}
@@ -264,6 +310,10 @@
 +	const char *value;
 +	int rewrite;
 +{
++#ifdef LEGACY_CRT1_ENVIRON
++	int ret;
++#endif /* LEGACY_CRT1_ENVIRON */
++
 +	/* no null ptr or empty str */
 +	if(name == NULL || *name == 0) {
 +		errno = EINVAL;
@@ -281,15 +331,15 @@
 +	if (*value == '=')			/* no `=' in value */
 +		++value;
 +	/* insure __zone0 is set up before calling __malloc_check_env_name */
-+	if (!__zone0) {
-+	    __zone0 = malloc_create_zone(0, 0);
-+	    if (!__zone0) {
-+		    errno = ENOMEM;
-+		    return (-1);
-+	    }
-+	}
++	if (init__zone0(1)) return (-1);
 +	__malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
++#ifdef LEGACY_CRT1_ENVIRON
++	ret = __setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0);
++	_saved_environ = *_NSGetEnviron();
++	return ret;
++#else /* !LEGACY_CRT1_ENVIRON */
 +	return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0));
++#endif /* !LEGACY_CRT1_ENVIRON */
 +}
 +
  /*
@@ -324,23 +374,13 @@
 +		return (-1);
 +	}
 +	/* insure __zone0 is set up before calling __malloc_check_env_name */
-+	if (!__zone0) {
-+	    __zone0 = malloc_create_zone(0, 0);
-+	    if (!__zone0) {
-+		    errno = ENOMEM;
-+		    return (-1);
-+	    }
-+	}
++	if (init__zone0(1)) return (-1);
 +#else /* !__DARWIN_UNIX03 */
 +	/* no null ptr or empty str */
 +	if(name == NULL || *name == 0)
 +		return;
 +	/* insure __zone0 is set up before calling __malloc_check_env_name */
-+	if (!__zone0) {
-+	    __zone0 = malloc_create_zone(0, 0);
-+	    if (!__zone0)
-+		    return;
-+	}
++	if (init__zone0(0)) return;
 +#endif /* __DARWIN_UNIX03 */
 +	__malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
 +	__unsetenv(name, *_NSGetEnviron(), __zone0);