Truncate builtin arguments on NUL

This restores the status quo where builtins are like external commands
in that they can't see anything after a 0x00, because that's the c-style
string terminator.
This commit is contained in:
Fabian Boehm 2023-06-23 16:29:42 +02:00
parent 41568eb2a8
commit c7b43b3abf
2 changed files with 16 additions and 2 deletions

View File

@ -153,7 +153,16 @@ fn rust_run_builtin(
status_code: &mut i32,
) -> bool {
let storage: Vec<WString> = cpp_args.from_ffi();
let mut args: Vec<&wstr> = storage.iter().map(|s| s.as_utfstr()).collect();
let mut args: Vec<&wstr> = storage
.iter()
.map(|s| match s.chars().position(|c| c == '\0') {
// We truncate on NUL for backwards-compatibility reasons.
// This used to be passed as c-strings (`wchar_t *`),
// and so we act like it for now.
Some(pos) => &s[..pos],
None => &s[..],
})
.collect();
let streams = &mut io_streams_t::new(streams);
match run_builtin(parser.unpin(), streams, args.as_mut_slice(), builtin) {

View File

@ -1,7 +1,12 @@
#RUN: %fish %s
# NUL-handling
# This one actually prints a NUL
echo (printf '%s\x00' foo bar | string escape)
# CHECK: foo\x00bar\x00
# This one is truncated
echo foo\x00bar | string escape
# CHECK: foo\x00bar
# CHECK: foo
# This one is just escaped
echo foo\\x00bar | string escape
# CHECK: foo\\x00bar