You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inline head fast path silently defaults invalid/missing/negative -n to 10 lines (flag parsing) #708
The inline head pipeline fast path (src/executor/pipeline_exec.rs:2865)
hand-rolls its option scan: it skips any unknown -… word, then calls head_line_count(&args).unwrap_or(10) (pipeline_exec.rs:3383). A malformed
count is therefore silently treated as "first 10 lines" with rc 0 instead of
an error, and a valid GNU negative count (-n -1, "all but the last line") is
lost because "-1".parse::<usize>() fails.
Classification: flag parsing. The arm should decline (→ external) whenever
the count operand is not a plain non-negative integer, rather than defaulting.
Minimal repro
$ niu -c "printf 'a\nb\nc\n' | head -n abc"; echo rc=$?
a
b
c
rc=0
$ <install>/winuxcmd/usr/bin/head.exe -n abc # rc=1: head: invalid number of lines: 'abc'
$ D:/Git/bin/bash.exe -c "printf 'a\nb\nc\n' | head -n abc" # rc=1, same message
repro
niu fast
external / GNU
printf 'a\nb\nc\n' | head -n abc
a\nb\nc\n rc 0
error, rc 1
printf 'a\nb\nc\n' | head -n -1
a\nb\nc\n
a\nb\n
printf 'a\nb\nc\n' | head -x
a\nb\nc\n rc 0
head: invalid option -- 'x', rc 1
printf 'a\nb\nc\n' | head -n
a\nb\nc\n rc 0
head: option requires an argument -- 'n', rc 1
printf 'a\nb\nc\n' | head --lines
a\nb\nc\n rc 0
head: option '--lines' requires an argument, rc 1
Inside a command substitution the same arm runs through the real executor:
$ niu -c "x=\$(printf 'a\nb\nc\n' | head -n -1); printf '%s' \"\$x\""
a
b
c # GNU: a\nb
Suggested fix
Only take the inline path for -n N / -nN / --lines=N / -N with a valid usize, and decline on -…/missing operand, so malformed shapes reach the real head. The dormant tail arm in command_substitution_values.rs:128 reuses head_line_count and would inherit the same defect if revived.
Activity
docs(audit): link filed issues from the fast-path catalogue and harness
docs(audit): fast-path differential catalogue and harness (niubash#383)
docs(audit): fast-path differential catalogue and harness (niubash#38…
fix: fast-path differential divergences #707/#708/#709 (tr carriers, …
caomengxuan666 commentedon Oct 17, 2026
已在
d4f0b67(PR #718)修掉,只是 issue 没跟着关。当前 masterd248d9f复验(精确取 rc,不经管道):printf 'a\nb\nc\n' | head -n abc... | head -n -1a\nb\n... | head -x... | head -n... | head --lines... | head -2a\nb\n非法/缺失操作数现在 decline 到真
head,负数走 GNU 的"除末尾 N 行"语义。剩下唯一的差别是诊断前缀带完整路径(/usr/bin/head:vshead:),那是外部命令 argv[0] 的另一件事,不在这个 issue 范围。