diff options
author | Jo-Philipp Wich <jo@mein.io> | 2024-09-20 09:40:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-20 09:40:58 +0200 |
commit | d0f423b60a5c0bf1ec3a374eda53420b2becf408 (patch) | |
tree | 7bcf670c1146f8cab546d273c15a1b6cd899bfe5 | |
parent | 6ea37c8fa4c4780ff99441ac787a2c9cd38c7bc6 (diff) | |
parent | 86f11211dc77cdd86209049692f83286dad73ad5 (diff) |
Merge pull request #221 from mikma/fix-getenv
lib: use copy of environ pointer in getenv()
-rw-r--r-- | lib.c | 9 | ||||
-rw-r--r-- | tests/custom/99_bugs/46_getenv_destroys_environ | 13 |
2 files changed, 18 insertions, 4 deletions
@@ -854,21 +854,22 @@ uc_getenv(uc_vm_t *vm, size_t nargs) { uc_value_t *key = uc_fn_arg(0), *rv = NULL; extern char **environ; + char **env = environ; char *k, *v; if (!key) { rv = ucv_object_new(vm); - while (*environ) { - v = strchr(*environ, '='); + while (*env) { + v = strchr(*env, '='); if (v) { - xasprintf(&k, "%.*s", (int)(v - *environ), *environ); + xasprintf(&k, "%.*s", (int)(v - *env), *env); ucv_object_add(rv, k, ucv_string_new(v + 1)); free(k); } - environ++; + env++; } } else if (ucv_type(key) == UC_STRING) { diff --git a/tests/custom/99_bugs/46_getenv_destroys_environ b/tests/custom/99_bugs/46_getenv_destroys_environ new file mode 100644 index 0000000..1879dee --- /dev/null +++ b/tests/custom/99_bugs/46_getenv_destroys_environ @@ -0,0 +1,13 @@ +A call to getenv() without parameters destroys environ, and subsequent calls +to getenv() (with or without parameter) return nothing. + +-- Testcase -- +{% + getenv(); + print(length(getenv()) > 0, '\n'); +%} +-- End -- + +-- Expect stdout -- +true +-- End -- |