Skip to content

Commit 7e201ca

Browse files
committed
CR
Signed-off-by: Mateusz Front <[email protected]>
1 parent d5ad0ec commit 7e201ca

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/libAtomVM/nifs.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,48 +1818,42 @@ static term nif_os_getenv_1(Context *ctx, int argc, term argv[])
18181818
int ok;
18191819
const char *env_var = interop_list_to_utf8_string(env_var_list, &ok);
18201820
if (UNLIKELY(!ok)) {
1821-
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
1821+
RAISE_ERROR(BADARG_ATOM);
18221822
}
18231823

18241824
#ifndef AVM_NO_SMP
18251825
smp_spinlock_lock(&ctx->global->env_spinlock);
18261826
#endif
18271827

1828-
const char *env_var_value_tmp = getenv(env_var);
1829-
1830-
if (IS_NULL_PTR(env_var_value_tmp)) {
1831-
result = FALSE_ATOM;
1832-
goto nif_os_getenv_1_unlock_return;
1833-
}
1834-
1835-
size_t env_value_len = strlen(env_var_value_tmp);
1836-
char *env_var_value = malloc(env_value_len + 1);
1828+
char *env_var_value = getenv(env_var);
18371829

1838-
if (UNLIKELY(IS_NULL_PTR(env_var_value))) {
1839-
result = OUT_OF_MEMORY_ATOM;
1840-
goto nif_os_getenv_1_unlock_return;
1830+
if (IS_NULL_PTR(env_var_value)) {
1831+
#ifndef AVM_NO_SMP
1832+
smp_spinlock_unlock(&ctx->global->env_spinlock);
1833+
#endif
1834+
return FALSE_ATOM;
18411835
}
18421836

1843-
strcpy(env_var_value, env_var_value_tmp);
1837+
env_var_value = strdup(env_var_value);
18441838

18451839
#ifndef AVM_NO_SMP
18461840
smp_spinlock_unlock(&ctx->global->env_spinlock);
18471841
#endif
18481842

1843+
if (UNLIKELY(IS_NULL_PTR(env_var_value))) {
1844+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
1845+
}
1846+
1847+
size_t env_value_len = strlen(env_var_value);
1848+
18491849
if (UNLIKELY(memory_ensure_free_opt(ctx, LIST_SIZE(env_value_len, 1), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1850+
free(env_var_value);
18501851
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
18511852
}
18521853

18531854
result = interop_bytes_to_list(env_var_value, env_value_len, &ctx->heap);
18541855
free(env_var_value);
18551856
return result;
1856-
1857-
nif_os_getenv_1_unlock_return:
1858-
#ifndef AVM_NO_SMP
1859-
smp_spinlock_unlock(&ctx->global->env_spinlock);
1860-
#endif
1861-
1862-
return result;
18631857
}
18641858

18651859
static term nif_erlang_make_tuple_2(Context *ctx, int argc, term argv[])

tests/libs/estdlib/test_os.erl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ test() ->
2929
ok.
3030

3131
test_os_getenv() ->
32-
true =
32+
ok =
3333
case atomvm:platform() of
34-
generic_unix ->
35-
is_list(os:getenv("PATH"));
36-
_ ->
37-
true
34+
generic_unix -> validate_path(os:getenv("PATH"));
35+
_ -> ok
36+
end,
37+
false = os:getenv("NON_EXISTENT_VAR"),
38+
false = os:getenv("ZAŻÓŁĆ_GĘŚLĄ_JAŹŃ"),
39+
false = os:getenv(""),
40+
ok =
41+
try
42+
os:getenv(not_a_string)
43+
catch
44+
error:badarg -> ok;
45+
Class:Reason:Stacktrace -> erlang:raise(Class, Reason, Stacktrace)
3846
end,
39-
false = os:getenv("NON_EXISTENT_PATH_VAR"),
4047
ok.
48+
49+
validate_path("/" ++ _Rest) -> ok;
50+
validate_path(_String) -> error.

0 commit comments

Comments
 (0)