Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace calls to putenv with setenv #18530

Merged
merged 7 commits into from
Jul 23, 2021
Merged

Conversation

brightly-salty
Copy link
Contributor

Fixes #18502 which could lead to memory leaks if left unchecked

in os.putenv(k, v), replaces calls to C putenv("k=v") with calls to C setenv(k, v, 1)

First time contributor here, let me know if anything else is needed of me

@timotheecour
Copy link
Member

this PR also improves #18533 but doesn't fix it; followup PR welcome to remove tracking of environment (more complex change so requires a different PR)

@juancarlospaco
Copy link
Collaborator

First time contributor here

Welcome!, nice code. 🙂:+1:

let me know if anything else is needed

If you are at it, maybe check the = Bug, in another PR.

@brightly-salty
Copy link
Contributor Author

Looks like the new behavior of os.putenv("foo=bar", "baz" will throw an OSError rather than setting foo to "". Is this correct or should it be changed?

@brightly-salty
Copy link
Contributor Author

See https://man7.org/linux/man-pages/man3/setenv.3.html#ERRORS, I could match on the error code to be able to throw a ValueError instead if EINVAL is returned.

@timotheecour
Copy link
Member

timotheecour commented Jul 19, 2021

@brightly-salty see my reply in timotheecour#782 (comment); your PR fixes timotheecour#782 ; please add a test using doAssertRaises(OSError)

I could match on the error code to be able to throw a ValueError instead if EINVAL is returned.

out of scope for this PR, and probably not a good idea; other OS calls don't do that

tests/stdlib/tos.nim Outdated Show resolved Hide resolved
tests/stdlib/tos.nim Outdated Show resolved Hide resolved
tests/stdlib/tos.nim Outdated Show resolved Hide resolved
brightly-salty and others added 3 commits July 19, 2021 22:16
Co-authored-by: Timothee Cour <[email protected]>
Co-authored-by: Timothee Cour <[email protected]>
tests/stdlib/tos.nim Outdated Show resolved Hide resolved
Copy link
Member

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

in future work, we could add an overload that exposes the overwrite param, it's a useful thing to be able to not overwrite if env var already exists; eg:

proc putEnv*(key, val: string, overwrite = true)

@Araq
Copy link
Member

Araq commented Jul 21, 2021

This needs to be tested on MSVC too, see also https://stackoverflow.com/questions/17258029/c-setenv-undefined-identifier-in-visual-studio

@Araq
Copy link
Member

Araq commented Jul 22, 2021

Note: I need to test this manually for MSCV before merging.

@Araq Araq merged commit f62f415 into nim-lang:devel Jul 23, 2021
@Araq
Copy link
Member

Araq commented Jul 23, 2021

Can confirm that the tests work on Windows with MSCV.

@brightly-salty brightly-salty deleted the fix-putenv branch July 23, 2021 13:14
@juancarlospaco
Copy link
Collaborator

Thanks for fixing it @brightly-salty 🙂

PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
* 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 <[email protected]>

* Update tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <[email protected]>

* Add comment with bug number to tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <[email protected]>

* Fix possible msvc arch issues

Co-authored-by: Timothee Cour <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

os.putEnv is buggy, and should use C setenv, not putenv
4 participants