Skip to content

Commit

Permalink
printf: Print special error for invalid octal numbers
Browse files Browse the repository at this point in the history
(tbh these were always a mistake)

See #9035
  • Loading branch information
faho committed Jun 23, 2022
1 parent d6d2c9c commit 13a9f6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/builtins/printf.cpp
Expand Up @@ -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);
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/checks/printf.fish
Expand Up @@ -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

0 comments on commit 13a9f6b

Please sign in to comment.