From 261811e0124d488984675ed187dc090281baafc2 Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 14:13:08 -0500 Subject: [PATCH 1/7] Replace calls to C `putenv` with C `setenv` to remove possible memory leaks --- lib/pure/includes/osenv.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pure/includes/osenv.nim b/lib/pure/includes/osenv.nim index d0c92d56611d..bbe96a90a54d 100644 --- a/lib/pure/includes/osenv.nim +++ b/lib/pure/includes/osenv.nim @@ -45,8 +45,8 @@ else: proc c_getenv(env: cstring): cstring {. importc: "getenv", header: "".} - proc c_putenv(env: cstring): cint {. - importc: "putenv", header: "".} + proc c_setenv(envname: cstring, envval: cstring, overwrite: cint): cint {. + importc: "setenv", header: "".} proc c_unsetenv(env: cstring): cint {. importc: "unsetenv", header: "".} @@ -221,7 +221,7 @@ else: else: if setEnvironmentVariableA(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].} = From 13a77f54fa264811fe9aa5b37c147d5e2620105a Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 17:03:09 -0500 Subject: [PATCH 2/7] Add test of correct behaviour on invalid input --- tests/stdlib/tos.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 912558461272..9f9692958f70 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -614,6 +614,10 @@ block osenv: doAssert existsEnv(dummyEnvVar) == false delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false + block putEnv: + # raises OSError on invalid input + 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", " ") == " " From 19c2c83e39883e507bfbe7c2df3ce0cec0f82c37 Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 22:16:00 +0000 Subject: [PATCH 3/7] Fix style in tests/stdlib/tos.nim Co-authored-by: Timothee Cour --- tests/stdlib/tos.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 9f9692958f70..757707f8fe01 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -614,7 +614,7 @@ block osenv: doAssert existsEnv(dummyEnvVar) == false delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false - block putEnv: + block: # putEnv # raises OSError on invalid input doAssertRaises(OSError, putEnv("DUMMY_ENV_VAR_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE")) doAssertRaises(OSError, putEnv("", "NEW_DUMMY_VALUE")) From d6acdcd69afbcc352e9731437bdc0189b0082d20 Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 22:16:14 +0000 Subject: [PATCH 4/7] Update tests/stdlib/tos.nim Co-authored-by: Timothee Cour --- tests/stdlib/tos.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 757707f8fe01..22bce74c6b83 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -615,7 +615,6 @@ block osenv: delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false block: # putEnv - # raises OSError on invalid input doAssertRaises(OSError, putEnv("DUMMY_ENV_VAR_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE")) doAssertRaises(OSError, putEnv("", "NEW_DUMMY_VALUE")) block: From f3f6412da5bd11d382ea6353b84a5dfe4feb96df Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 22:16:35 +0000 Subject: [PATCH 5/7] Update tests/stdlib/tos.nim Co-authored-by: Timothee Cour --- tests/stdlib/tos.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 22bce74c6b83..cc2cf7ce9a3c 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -615,8 +615,8 @@ block osenv: delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false block: # putEnv - doAssertRaises(OSError, putEnv("DUMMY_ENV_VAR_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE")) - doAssertRaises(OSError, putEnv("", "NEW_DUMMY_VALUE")) + 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", " ") == " " From 3653b9e06afc58d158d2bb3aa5fb2bf4202a63cb Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Mon, 19 Jul 2021 22:22:14 +0000 Subject: [PATCH 6/7] Add comment with bug number to tests/stdlib/tos.nim Co-authored-by: Timothee Cour --- tests/stdlib/tos.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index cc2cf7ce9a3c..8a12b9b9066e 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -614,7 +614,7 @@ block osenv: doAssert existsEnv(dummyEnvVar) == false delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false - block: # putEnv + block: # putEnv, bug #18502 doAssertRaises(OSError): putEnv("DUMMY_ENV_VAR_PUT=DUMMY_VALUE", "NEW_DUMMY_VALUE") doAssertRaises(OSError): putEnv("", "NEW_DUMMY_VALUE") block: From d1d486ba8e99eefb73f4aeb30295806a7e203fc8 Mon Sep 17 00:00:00 2001 From: Caden Haustein Date: Wed, 21 Jul 2021 09:45:43 -0500 Subject: [PATCH 7/7] Fix possible msvc arch issues --- lib/pure/includes/osenv.nim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/pure/includes/osenv.nim b/lib/pure/includes/osenv.nim index bbe96a90a54d..db291c5fd377 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: "".} - proc c_setenv(envname: cstring, envval: cstring, overwrite: cint): cint {. - importc: "setenv", header: "".} + when defined(vcc): + proc c_putenv_s(envname: cstring, envval: cstring): cint {.importc: "_putenv_s", header: "".} + else: + proc c_setenv(envname: cstring, envval: cstring, overwrite: cint): cint {.importc: "setenv", header: "".} proc c_unsetenv(env: cstring): cint {. importc: "unsetenv", header: "".} @@ -220,6 +222,9 @@ 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_setenv(key, val, 1'i32) != 0'i32: raiseOSError(osLastError())