Skip to content

Commit

Permalink
Merge pull request #221 from mikma/fix-getenv
Browse files Browse the repository at this point in the history
lib: use copy of environ pointer in getenv()
  • Loading branch information
jow- authored Sep 20, 2024
2 parents 6ea37c8 + 86f1121 commit d0f423b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/custom/99_bugs/46_getenv_destroys_environ
Original file line number Diff line number Diff line change
@@ -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 --

0 comments on commit d0f423b

Please sign in to comment.