Prevent NPE after clearenv (#542)

This commit is contained in:
Gavin Hayes 2022-08-17 17:15:23 -04:00 committed by GitHub
parent e5f705ace3
commit 1f735a4af3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,10 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/mem/alg.h"
#include "libc/calls/strace.internal.h" #include "libc/calls/strace.internal.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/mem/internal.h" #include "libc/mem/internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
@ -57,12 +57,14 @@ static void GrowEnviron(void) {
size_t n, c; size_t n, c;
char **a, **b, **p; char **a, **b, **p;
a = environ; a = environ;
n = GetEnvironLen(a); n = a ? GetEnvironLen(a) : 0;
c = MAX(16ul, n) << 1; c = MAX(16ul, n) << 1;
b = calloc(c, sizeof(char *)); b = calloc(c, sizeof(char *));
if (a) {
for (p = b; *a;) { for (p = b; *a;) {
*p++ = strdup(*a++); *p++ = strdup(*a++);
} }
}
__cxa_atexit(FreeEnviron, b, 0); __cxa_atexit(FreeEnviron, b, 0);
environ = b; environ = b;
capacity = c; capacity = c;
@ -75,6 +77,8 @@ int PutEnvImpl(char *s, bool overwrite) {
__cxa_atexit(RestoreOriginalEnvironment, environ, 0); __cxa_atexit(RestoreOriginalEnvironment, environ, 0);
GrowEnviron(); GrowEnviron();
once = true; once = true;
} else if (!environ) {
GrowEnviron();
} }
for (p = s; *p && *p != '='; ++p) { for (p = s; *p && *p != '='; ++p) {
if (IsWindows()) { if (IsWindows()) {