jow- / ucode

JavaScript-like language with optional templating
ISC License
90 stars 33 forks source link

getenv() call destroys "environ" #219

Closed mikma closed 1 month ago

mikma commented 1 month ago

A call to getenv() without parameters destroys environ, and subsequent calls to getenv() (with or without parameter) return nothing.

Test case:

#!/bin/ucode

assert(length(getenv()) != 0, 'first call should succeed.');
assert(length(getenv()) != 0, 'second call should succeed.');
print('PASS\n');

Proposed fix:

Index: ucode-2024.07.11~1a8a0bcf/lib.c
===================================================================
--- ucode-2024.07.11~1a8a0bcf.orig/lib.c
+++ ucode-2024.07.11~1a8a0bcf/lib.c
@@ -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) {
dangowrt commented 1 month ago

Please open a pull-request with two commits: