This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
Fix getEnvFromShell() to correctly handle newlines in env vars #17628
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
Fix #17627 by using awk to portably separate env vars with \0 instead of \n.
getEnvFromShell() was supposed to copy the user's environment as it exists in a new shell. However, it did that by running
env
in that shell and using \n to split the output into var=value pairs. However, values can contain \n (e.g. exported bash functions) and such values will break the parsing. The fix is to have the var=value pairs separated by \0s. GNU'senv -0
does that but won't work on Mac or BSD. Instead, we get the same result using a one line POSIX-compliantawk
script.Alternate Designs
I was going to just use
env -0
until I realized that it was GNU-specific.Why Should This Be In Core?
It fixes a bug in core.
Benefits
It fixes and prevents issues where a subprocess is started with an incorrect environment due to multiline environment variables.
Possible Drawbacks
If
awk
corresponds to GNU'sawk
, it will add AWKLIBPATH and AWKPATH to the environment (with default values) if they don't already exist.Verification Process
I modified the existing spec to include multiline values and confirmed it worked.
I also confirmed that multiline values set in .bashrc are actually showing up correctly in process.env after the change was made.
Applicable Issues
Issue #17627