@@ -1818,48 +1818,42 @@ static term nif_os_getenv_1(Context *ctx, int argc, term argv[])
1818
1818
int ok ;
1819
1819
const char * env_var = interop_list_to_utf8_string (env_var_list , & ok );
1820
1820
if (UNLIKELY (!ok )) {
1821
- RAISE_ERROR (OUT_OF_MEMORY_ATOM );
1821
+ RAISE_ERROR (BADARG_ATOM );
1822
1822
}
1823
1823
1824
1824
#ifndef AVM_NO_SMP
1825
1825
smp_spinlock_lock (& ctx -> global -> env_spinlock );
1826
1826
#endif
1827
1827
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 );
1837
1829
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 ;
1841
1835
}
1842
1836
1843
- strcpy ( env_var_value , env_var_value_tmp );
1837
+ env_var_value = strdup ( env_var_value );
1844
1838
1845
1839
#ifndef AVM_NO_SMP
1846
1840
smp_spinlock_unlock (& ctx -> global -> env_spinlock );
1847
1841
#endif
1848
1842
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
+
1849
1849
if (UNLIKELY (memory_ensure_free_opt (ctx , LIST_SIZE (env_value_len , 1 ), MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
1850
+ free (env_var_value );
1850
1851
RAISE_ERROR (OUT_OF_MEMORY_ATOM );
1851
1852
}
1852
1853
1853
1854
result = interop_bytes_to_list (env_var_value , env_value_len , & ctx -> heap );
1854
1855
free (env_var_value );
1855
1856
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 ;
1863
1857
}
1864
1858
1865
1859
static term nif_erlang_make_tuple_2 (Context * ctx , int argc , term argv [])
0 commit comments