aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRickard Andersson <gonz@severnatazvezda.com>2023-06-27 21:37:10 +0300
committerRickard Andersson <gonz@severnatazvezda.com>2023-06-27 21:37:10 +0300
commit330b393e1604b9a41fb8751b6446f73091c9f85c (patch)
tree71f01f994632016d91af9f9801764bcc9c410cd6
parent8b8310711e4924678e605278fca1f8dbf517b903 (diff)
fix(os): use `setenv` instead of `putenv`
`setenv` doesn't copy the value that is put, which means that the previous code had a bug where we free'd the temporary memory and the environment was accidentally cleared right after the function finished.
-rw-r--r--core/os/os_linux.odin6
1 files changed, 5 insertions, 1 deletions
diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin
index e0b60fd36..aaff01165 100644
--- a/core/os/os_linux.odin
+++ b/core/os/os_linux.odin
@@ -458,6 +458,7 @@ foreign libc {
@(link_name="execvp") _unix_execvp :: proc(path: cstring, argv: [^]cstring) -> int ---
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
@(link_name="putenv") _unix_putenv :: proc(cstring) -> c.int ---
+ @(link_name="setenv") _unix_setenv :: proc(key: cstring, value: cstring, overwrite: c.int) -> c.int ---
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
@@ -894,7 +895,10 @@ get_env :: proc(key: string, allocator := context.allocator) -> (value: string)
set_env :: proc(key, value: string) -> Errno {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
s := strings.concatenate({key, "=", value, "\x00"}, context.temp_allocator)
- res := _unix_putenv(strings.unsafe_string_to_cstring(s))
+ key_cstring := strings.unsafe_string_to_cstring(strings.concatenate({key, "\x00"}, context.temp_allocator))
+ value_cstring := strings.unsafe_string_to_cstring(strings.concatenate({value, "\x00"}, context.temp_allocator))
+ // NOTE(GoNZooo): `setenv` instead of `putenv` because it copies both key and value more commonly
+ res := _unix_setenv(key_cstring, value_cstring, 1)
if res < 0 {
return Errno(get_last_error())
}