patch-shebangs: fix crash with shebang without trailing newline
This fixes a bug where `patchShebangs` crashes when trying to patch
files that contain only a shebang (e.g. `#!/bin/bash`) (and nothing
else) and do not end with a newline.
Such file can be produced using `printf "#!/bin/bash" > example` or
`echo -n "#!/bin/bash" > example`.
I don't understand why one would want to create such files, as they do
literally nothing, but the chromium tarball we are using started
shipping some 🫠
Full reproducer:
```nix
with import <nixpkgs> { };
stdenv.mkDerivation {
dontUnpack = true;
name = "patch-shebangs-no-trailing-newline-reproducer";
postPatch = ''
printf "#!/bin/bash" > reproducer
chmod +x reproducer
patchShebangs reproducer
'';
}
```
```
❯ nix-build reproducer.nix
this derivation will be built:
/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv
building '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'...
patching sources
patching script interpreter paths in reproducer
/nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
error: builder for '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv' failed with exit code 1;
last 3 log lines:
> patching sources
> patching script interpreter paths in reproducer
> /nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
For full logs, run 'nix log /nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'.
```
This commit is contained in:
parent
7f93574706
commit
c47f245253
|
@ -72,7 +72,10 @@ patchShebangs() {
|
|||
while IFS= read -r -d $'\0' f; do
|
||||
isScript "$f" || continue
|
||||
|
||||
read -r oldInterpreterLine < "$f"
|
||||
# read exits unclean if the shebang does not end with a newline, but still assigns the variable.
|
||||
# So if read returns errno != 0, we check if the assigned variable is non-empty and continue.
|
||||
read -r oldInterpreterLine < "$f" || [ "$oldInterpreterLine" ]
|
||||
|
||||
read -r oldPath arg0 args <<< "${oldInterpreterLine:2}"
|
||||
|
||||
if [[ -z "${pathName:-}" ]]; then
|
||||
|
|
|
@ -72,11 +72,26 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
without-trailing-newline = stdenv.mkDerivation {
|
||||
name = "without-trailing-newline";
|
||||
strictDeps = false;
|
||||
dontUnpack = true;
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
printf "#!/bin/bash" > $out/bin/test
|
||||
chmod +x $out/bin/test
|
||||
dontPatchShebangs=
|
||||
'';
|
||||
passthru = {
|
||||
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
in
|
||||
stdenv.mkDerivation {
|
||||
name = "test-patch-shebangs";
|
||||
passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string; };
|
||||
passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string without-trailing-newline; };
|
||||
buildCommand = ''
|
||||
validate() {
|
||||
local name=$1
|
||||
|
|
Loading…
Reference in New Issue
Block a user