-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
os/exec: Cmd fails to enforce that Env entries are of the documented "key=value" form #52436
Comments
I would like to note here that the FOO=bar form of environment strings is merely a convention on most Unix systems. (https://man7.org/linux/man-pages/man7/environ.7.html) The C execve function does also not enforce this convention (https://man7.org/linux/man-pages/man2/execve.2.html). Therefore, it is also not strictly necessary that the Go exec package enforces this convention. Actually, we might be calling programs that use the environment in non conventional ways, which will not be possible anymore if exec enforced the convention. I would propose to correct the documentation to reflect this |
@beoran, you are correct about the Unix format being strictly a convention — however, the I agree that in principle this issue could be resolve by updating the documentation. |
@bcmills If we look at os.Environ we can readily see that it does not enforce the = convention. It might be convenient that exec.Cmd does sometimes enforce the convention, but it is not consistent with os.Environ's actual implementation. So it seems a bit of a difficult situation. Even though it is not documented, there will likely be some Go programs that rely on the actual stlib behavior. Fixing the docs seems like the least invasive solution. Of course, if I understand correctly, the Go compatibility promise does not apply to undocumented behavior, so it could also be solved by fixing the various environment related functions to enforce the convention. I'll leave it up to the Go maintainers to decide. |
Change https://go.dev/cl/401339 mentions this issue: |
Once #50599 is implemented, the entries will be observable via the Environ method. I find it confusing for later entries in the list to jump arbitrarily far forward based on entries for the same key that no longer exist. This also fixes the deduplication logic for the degenerate Windows keys observed in #49886, which were previously deduplicated as empty keys. (It does not do anything about the even-more-degenerate keys observed in #52436.) For #50599. Change-Id: Ia7cd2200ec34ccc4b9d18631cb513194dc420c25 Reviewed-on: https://go-review.googlesource.com/c/go/+/401339 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
I believe these variables without = signs are permitted on Unix although certainly non-standard. I wouldn't want to disallow them on Unix in case we are running in a parent process that has inherited one of these oddities. If we disallow them then code using os/exec and passing through os.Environ() would start failing. On Windows, environment variables without = cause exec to fail with an unhelpful "The parameter is incorrect." error. |
In #61956 (comment), @zyga points out that if non- |
(Noticed while investigating #49886 in the context of #50599.)
The documentation for the
Env
field of theexec.Cmd
struct says:That is consistent with
os.Environ()
, which is documented as:Unfortunately, the implementation of
os/exec.Cmd
violates both of those documented behaviors.As demonstrated in https://go.dev/play/p/qdlwGaJg1_f:
=
are erroneously deduplicated. (The key=C:
collapses with the key=D:
, dropping the=C:
entry entirely.)"garbage"
is passed through toos.Environ()
without producing an error.)The text was updated successfully, but these errors were encountered: