From f284cdce5bf81bbe60f8406cc3c49862e84ad7aa Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Thu, 3 Mar 2022 12:12:04 +0100 Subject: [PATCH] Make byte/unicode escapes with no digits a tokenizer error This is the simple fix - if we have no valid digit, we have nothing to return. So instead of returning a NULL, we return an error. This is already the case for invalid octal escapes (like `\777`). Fixes #8545 --- src/common.cpp | 4 +++- tests/checks/basic.fish | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common.cpp b/src/common.cpp index ccd8d803d..38313c6c2 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1219,6 +1219,8 @@ maybe_t read_unquoted_escape(const wchar_t *input, wcstring *result, boo for (size_t i = 0; i < chars; i++) { long d = convert_digit(input[in_pos], base); if (d < 0) { + // If we have no digit, this is a tokenizer error. + if (i == 0) errored = true; break; } @@ -1226,7 +1228,7 @@ maybe_t read_unquoted_escape(const wchar_t *input, wcstring *result, boo in_pos++; } - if (res <= max_val) { + if (!errored && res <= max_val) { result_char_or_none = static_cast((byte_literal ? ENCODE_DIRECT_BASE : 0) + res); } else { diff --git a/tests/checks/basic.fish b/tests/checks/basic.fish index 3befee0ed..cfade8f88 100644 --- a/tests/checks/basic.fish +++ b/tests/checks/basic.fish @@ -558,3 +558,13 @@ end # CHECKERR: for PWD in foo bar # CHECKERR: ^ # XXX FIXME carat should point at PWD + +$fish -c 'echo \xtest' +# CHECKERR: fish: Invalid token '\xtest' +# CHECKERR: echo \xtest +# CHECKERR: ^ + +$fish -c 'echo \utest' +# CHECKERR: fish: Invalid token '\utest' +# CHECKERR: echo \utest +# CHECKERR: ^