Repository navigation
[core] Fix shell detection with multiline commands - #23747
jinchuntan wants to merge 1 commit into
Conversation
A newline in a shell argument makes cut return text from later lines. This can select the wrong setup path and leave ROOTSYS unset. Read the first line after converting the NUL separators to newlines. Keep reading the stream so long commands also work with pipefail. Add regression coverage for bash and zsh. Fixes root-project#23746 Assisted-by: codex:gpt-6
Test Results 24 files 24 suites 4d 1h 10m 10s ⏱️ For more details on these failures, see this check. Results for commit 8f8c0e7. |
| # Determine the shell executable filename. | ||
| if [ -r "/proc/$$/cmdline" ]; then | ||
| trueExe=$(cut -d '' -f1 /proc/$$/cmdline 2>/dev/null) || trueExe=$(xargs -0 -n 1 < /proc/$$/cmdline | head -n 1) || return 1 | ||
| trueExe=$(tr '\0' '\n' < /proc/$$/cmdline 2>/dev/null | sed -n '1p') || trueExe=$(xargs -0 -n 1 < /proc/$$/cmdline | head -n 1) || return 1 |
There was a problem hiding this comment.
Would it also work to do instead:
head -n 1 /proc/$$/cmdline 2>/dev/null | cut -d '' -f1
or does that file with long commands also?
as suggested by mkelement0 in https://stackoverflow.com/a/23011530/7471760
and what about basename -- "$(readlink /proc/$$/exe)"
There was a problem hiding this comment.
I guess the head alternative would work, but it keeps relying on cut -d '' meaning “NUL delimiter”, which is a GNU-coreutils quirk, while tr '\0' '\n' is plain POSIX. I’d keep the PR’s version for that reason.
basename -- "$(readlink /proc/$$/exe)" is a semantic change, because it follows symlinks. Interesting suggestion, and maybe this is actually what we want, but this would be a separate discussion independent of this bugfix PR.
|
Given the absence of PR author response and the trivial nature of the fix, I just re-opened a PR with the one-line fix myself: I also dropped the added test, because the suggested test only runs when zsh is available and is therefore risking to rot fast, like any other test that requires optional dependencies. The |
This pull request
Changes or fixes
Fixes #23746.
A newline in a shell argument makes
cutinclude text from later lines. This can sendthisroot.shthrough the wrong setup branch and leaveROOTSYSunset.This follows the approach suggested in the issue. It converts the NUL separators to newlines and prints the first line. Using
sedkeeps the input flowing so long commands also work withpipefailenabled.The regression test covers bash and zsh with single line commands and multiline commands. It also checks a long command with
pipefailenabled.Checklist
The original version failed the multiline cases. The
headversion failed the long command cases. The final change passed all six cases.The workspace reports process IDs that differ from its
/procentries. The temporary fixtures were adapted to read the real process ID. The committed test has no such adaptation. A full ROOT build and a run on a normal Linux system are still unverified.Codex with GPT 6 helped prepare the patch and tests.