mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-19 05:14:43 +00:00
Prevent NPE after clearenv (#542)
This commit is contained in:
parent
e5f705ace3
commit
1f735a4af3
1 changed files with 8 additions and 4 deletions
|
@ -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()) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue