From 13a9f6b64e81cb548469edf42102167c2770d3ac Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Thu, 23 Jun 2022 18:12:13 +0200 Subject: [PATCH] printf: Print special error for invalid octal numbers (tbh these were always a mistake) See #9035 --- src/builtins/printf.cpp | 6 ++++++ tests/checks/printf.fish | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/builtins/printf.cpp b/src/builtins/printf.cpp index b348097c5..5e0e28d93 100644 --- a/src/builtins/printf.cpp +++ b/src/builtins/printf.cpp @@ -169,6 +169,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