Skip to content

Commit c3770fc

Browse files
acfoltzersunfishcode
authored andcommitted
Fix length of environ array, and short-circuit for 0-len args/env
1 parent f817d04 commit c3770fc

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

basics/libc/crt1.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ static __wasi_errno_t populate_args(size_t *argc, char **argv) {
1717
if (err != __WASI_ESUCCESS) {
1818
return err;
1919
}
20+
if (*argc == 0) {
21+
return __WASI_ESUCCESS;
22+
}
2023

2124
/* Allocate memory for the array of pointers. */
2225
argv = malloc(sizeof(char *) * *argc);
@@ -40,9 +43,14 @@ static __wasi_errno_t populate_environ() {
4043
if (err != __WASI_ESUCCESS) {
4144
return err;
4245
}
46+
/* If there's no environment pairs, make sure environ is null and return. */
47+
if (environ_count == 0) {
48+
environ = NULL;
49+
return __WASI_ESUCCESS;
50+
}
4351

44-
/* Allocate memory for the array of pointers. */
45-
environ = malloc(sizeof(char *) * environ_count);
52+
/* Allocate memory for the array of pointers, plus one terminating null pointer. */
53+
environ = malloc(sizeof(char *) * (environ_count + 1));
4654
/* Allocate memory for storing the environment chars. */
4755
char *environ_buf = malloc(sizeof(char) * environ_size);
4856
if (environ == NULL || environ_buf == NULL) {

0 commit comments

Comments
 (0)