Fix 64-bit integer terminal write where high bit set

Using strtoll() can only return numbers in the range [-2^63, 2^63-1]. This
means that 0xffffFFFFffffFFFF (2^64-1) will be out of range and is written as
max LL. Actually, every 64-bit number with high-bit set will wrongly be
written as max LL.

This commit uses strtoull() instead to fix this, and checks for unsiged out-
of-range error. strtoull() also has the neat benefit that input with a minus
sign is treated like C unsigned numbers, ie, -u is also a valid unsigned
number if only u is one. In case the input is meant to be treated as signed,
it is therefore still OK to use strtoull() in the first instance only that in
this case a second check against the range of the signed domain is necessary.
This commit is contained in:
Stefan Rueger 2022-07-12 11:19:47 +01:00
parent 9afa56381e
commit 62d3eebd56
1 changed files with 4 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <errno.h>
#if defined(HAVE_LIBREADLINE) #if defined(HAVE_LIBREADLINE)
# include <readline/readline.h> # include <readline/readline.h>
@ -470,8 +471,9 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
} }
// Try integers // Try integers
data.ll = strtoll(argi, &end_ptr, 0); errno = 0;
if (*end_ptr || (end_ptr == argi)) { data.ll = strtoull(argi, &end_ptr, 0);
if (!(end_ptr == argi || errno)) {
// Try float // Try float
data.f = strtof(argi, &end_ptr); data.f = strtof(argi, &end_ptr);
data.is_float = true; data.is_float = true;