From: Dmitry Volyntsev Date: Fri, 30 Sep 2022 01:48:09 +0000 (-0700) Subject: Improved population of process.env object. X-Git-Tag: 0.7.8~9 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=6977555df73d2570de23afff9f3592d8f5a4c624;p=njs.git Improved population of process.env object. 1) Keys are always casted to upper case. 2) Keys and values are converted to safe Unicode strings. --- diff --git a/src/njs_builtin.c b/src/njs_builtin.c index 825ec903..65350e5b 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -107,9 +107,6 @@ static const njs_object_type_init_t *const }; -extern char **environ; - - njs_inline njs_int_t njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, const njs_object_init_t *init) @@ -1793,9 +1790,13 @@ static njs_int_t njs_env_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, char **environment) { char **ep; - u_char *val, *entry; + u_char *dst; + ssize_t length; + uint32_t cp; njs_int_t ret; + const u_char *val, *entry, *s, *end; njs_object_prop_t *prop, *prev; + njs_string_prop_t string; njs_lvlhsh_query_t lhq; lhq.replace = 0; @@ -1818,14 +1819,28 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, char **environment) continue; } - ret = njs_string_set(vm, &prop->name, entry, val - entry); + ret = njs_string_create(vm, &prop->name, (char *) entry, val - entry); if (njs_slow_path(ret != NJS_OK)) { return NJS_ERROR; } + (void) njs_string_prop(&string, &prop->name); + + length = string.length; + s = string.start; + end = s + string.size; + dst = (u_char *) s; + + while (length != 0) { + cp = njs_utf8_upper_case(&s, end); + dst = njs_utf8_encode(dst, cp); + length--; + } + val++; - ret = njs_string_set(vm, &prop->value, val, njs_strlen(val)); + ret = njs_string_create(vm, &prop->value, (char *) val, + njs_strlen(val)); if (njs_slow_path(ret != NJS_OK)) { return NJS_ERROR; } diff --git a/src/njs_unix.h b/src/njs_unix.h index bfba7e75..efcea754 100644 --- a/src/njs_unix.h +++ b/src/njs_unix.h @@ -47,6 +47,8 @@ #include +extern char **environ; + #if defined(PATH_MAX) #define NJS_MAX_PATH PATH_MAX #else diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 8cc6d9f7..038ea3a9 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -13877,6 +13877,9 @@ static njs_unit_test_t njs_test[] = { njs_str("Object.values(process)"), njs_str("") }, + { njs_str("Object.keys(process.env).sort()"), + njs_str("DUP,TZ") }, + { njs_str("Object.values()"), njs_str("TypeError: cannot convert undefined argument to object") }, @@ -24160,9 +24163,14 @@ main(int argc, char **argv) return (ret == NJS_DONE) ? EXIT_SUCCESS: EXIT_FAILURE; } + environ = NULL; + (void) putenv((char *) "TZ=UTC"); tzset(); + (void) putenv((char *) "DUP=bar"); + (void) putenv((char *) "dup=foo"); + njs_mm_denormals(1); njs_memzero(&stat, sizeof(njs_stat_t));