From 89a30841f2600b749319a7c5b4e9d82ea6ea15ed Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Wed, 13 Jul 2022 21:51:29 +0800 Subject: [PATCH] printf: Print special error for invalid octal numbers (tbh these were always a mistake) See #9035 (cherry picked from commit 13a9f6b64e81cb548469edf42102167c2770d3ac) --- CHANGELOG.rst | 1 + src/builtins/printf.cpp | 6 ++++++ tests/checks/printf.fish | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 04aeaa3c5..b2781824a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ fish 3.5.1 (released ???) This release of fish introduces the following small enhancements: - Cursor shaping for Vi mode is enabled by default in tmux, and will be used if the outer terminal is capable (:issue:`8981`). +- ``printf`` returns a better error when used with arguments interpreted as octal numbers (:issue:`9035`). This release also fixes a number of problems identified in fish 3.5.0. diff --git a/src/builtins/printf.cpp b/src/builtins/printf.cpp index d5b8e81aa..338e69a4c 100644 --- a/src/builtins/printf.cpp +++ b/src/builtins/printf.cpp @@ -263,6 +263,12 @@ void builtin_printf_state_t::verify_numeric(const wchar_t *s, const wchar_t *end } else { // This isn't entirely fatal - the value should still be printed. this->nonfatal_error(_(L"%ls: value not completely converted (can't convert '%ls')"), s, end); + // Warn about octal numbers as they can be confusing. + // Do it if the unconverted digit is a valid hex digit, + // because it could also be an "0x" -> "0" typo. + if (*s == L'0' && iswxdigit(*end)) { + this->nonfatal_error(_(L"Hint: a leading '0' without an 'x' indicates an octal number"), s, end); + } } } } diff --git a/tests/checks/printf.fish b/tests/checks/printf.fish index 5c6f13405..6b7d52795 100644 --- a/tests/checks/printf.fish +++ b/tests/checks/printf.fish @@ -95,3 +95,27 @@ printf '%d\n' 15.1 # CHECKERR: 15.1: value not completely converted (can't convert '.1') echo $status # CHECK: 1 + +printf '%d\n' 07 +# CHECK: 7 +echo $status +# CHECK: 0 +printf '%d\n' 08 +# CHECK: 0 +# CHECKERR: 08: value not completely converted (can't convert '8') +# CHECKERR: Hint: a leading '0' without an 'x' indicates an octal number +echo $status +# CHECK: 1 + +printf '%d\n' 0f +# CHECK: 0 +# CHECKERR: 0f: value not completely converted (can't convert 'f') +# CHECKERR: Hint: a leading '0' without an 'x' indicates an octal number +echo $status +# CHECK: 1 + +printf '%d\n' 0g +# CHECK: 0 +# CHECKERR: 0g: value not completely converted (can't convert 'g') +echo $status +# CHECK: 1