diff options
author | Caden Haustein <code@brightlysalty.33mail.com> | 2021-07-23 08:04:29 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-23 10:04:29 +0200 |
commit | f62f4159f8c793ba24fa214b26c4dc68a530bc2e (patch) | |
tree | 60212e7ef8ee01ef0b6bf9ba877ac26c33e07f64 | |
parent | 5386ae75ba91ee2fe280f1fe807676a7c9af4b13 (diff) | |
download | Nim-f62f4159f8c793ba24fa214b26c4dc68a530bc2e.tar.gz |
Replace calls to `putenv` with `setenv` (#18530)
* Replace calls to C `putenv` with C `setenv` to remove possible memory leaks * Add test of correct behaviour on invalid input * Fix style in tests/stdlib/tos.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Update tests/stdlib/tos.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Update tests/stdlib/tos.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Add comment with bug number to tests/stdlib/tos.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Fix possible msvc arch issues Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
-rw-r--r-- | lib/pure/includes/osenv.nim | 11 | ||||
-rw-r--r-- | tests/stdlib/tos.nim | 3 |
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/pure/includes/osenv.nim b/lib/pure/includes/osenv.nim index d0c92d566..db291c5fd 100644 --- a/lib/pure/includes/osenv.nim +++ b/lib/pure/includes/osenv.nim @@ -45,8 +45,10 @@ else: proc c_getenv(env: cstring): cstring {. importc: "getenv", header: "<stdlib.h>".} - proc c_putenv(env: cstring): cint {. - importc: "putenv", header: "<stdlib.h>".} + when defined(vcc): + proc c_putenv_s(envname: cstring, envval: cstring): cint {.importc: "_putenv_s", header: "<stdlib.h>".} + else: + proc c_setenv(envname: cstring, envval: cstring, overwrite: cint): cint {.importc: "setenv", header: "<stdlib.h>".} proc c_unsetenv(env: cstring): cint {. importc: "unsetenv", header: "<stdlib.h>".} @@ -220,8 +222,11 @@ else: if setEnvironmentVariableW(k, v) == 0'i32: raiseOSError(osLastError()) else: if setEnvironmentVariableA(key, val) == 0'i32: raiseOSError(osLastError()) + elif defined(vcc): + if c_putenv_s(key, val) != 0'i32: + raiseOSError(osLastError()) else: - if c_putenv(environment[indx]) != 0'i32: + if c_setenv(key, val, 1'i32) != 0'i32: raiseOSError(osLastError()) proc delEnv*(key: string) {.tags: [WriteEnvEffect].} = diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 912558461..8a12b9b90 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -614,6 +614,9 @@ block osenv: doAssert existsEnv(dummyEnvVar) == false delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false + block: # putEnv, bug #18502 + doAssertRaises(OSError): putEnv("DUMMY_ENV_VAR_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE") + doAssertRaises(OSError): putEnv("", "NEW_DUMMY_VALUE") block: doAssert getEnv("DUMMY_ENV_VAR_NONEXISTENT", "") == "" doAssert getEnv("DUMMY_ENV_VAR_NONEXISTENT", " ") == " " |