Implement setenv correctly and support setting values to empty strings
authorChristopher Dykes <cdykes@fb.com>
Tue, 9 Aug 2016 23:34:41 +0000 (16:34 -0700)
committerFacebook Github Bot 1 <facebook-github-bot-1-bot@fb.com>
Tue, 9 Aug 2016 23:38:29 +0000 (16:38 -0700)
commit5d8ca09a3f96afefb44e35808f03651a096ab9c7
tree5b5f3ed5f7e5e30747e226b00afc292a9231454a
parent4d777293b5993289afd023c43442833e3f2ac2eb
Implement setenv correctly and support setting values to empty strings

Summary: Just calling `SetEnvironmentVariableA` wasn't updating `_environ`, which meant that calls to `getenv` weren't reflecting the changes made via `setenv`. The correct way to implement it is using `_putenv_s`, but there's one problem with that: passing an empty string as the value to `_putenv_s` results in the environment variable being unset. To make it possible to set the environment variable to an empty string, we shall dive head-first into the implementation details of the CRT and emerge victorious by blatently ignoring the documentation of `getenv` and modifying the string it returns to terminate it early.

Reviewed By: yfeldblum

Differential Revision: D3691007

fbshipit-source-id: 350c2ec72ec90b9178a9a45b2c2ed2659b788e37
folly/experimental/test/TestUtilTest.cpp
folly/portability/Environment.cpp