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
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/alg.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/mem/internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -57,11 +57,13 @@ static void GrowEnviron(void) {
size_t n, c;
char **a, **b, **p;
a = environ;
n = GetEnvironLen(a);
n = a ? GetEnvironLen(a) : 0;
c = MAX(16ul, n) << 1;
b = calloc(c, sizeof(char *));
for (p = b; *a;) {
*p++ = strdup(*a++);
if (a) {
for (p = b; *a;) {
*p++ = strdup(*a++);
}
}
__cxa_atexit(FreeEnviron, b, 0);
environ = b;
@ -75,6 +77,8 @@ int PutEnvImpl(char *s, bool overwrite) {
__cxa_atexit(RestoreOriginalEnvironment, environ, 0);
GrowEnviron();
once = true;
} else if (!environ) {
GrowEnviron();
}
for (p = s; *p && *p != '='; ++p) {
if (IsWindows()) {