From 62d3eebd568c2078428a55485675a8411d97f415 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@urclocks.com>
Date: Tue, 12 Jul 2022 11:19:47 +0100
Subject: [PATCH] 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.
---
 src/term.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

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