the attached patch gives us your nice "%lx>%d bytes" error for all cases.

On Mon, Oct 9, 2023 at 9:42 PM Rob Landley <r...@landley.net> wrote:
>
> On 10/9/23 17:04, enh wrote:
> > On Mon, Oct 9, 2023 at 2:45 PM Rob Landley <r...@landley.net> wrote:
> >>
> >> On 10/9/23 10:26, enh wrote:
> >> >> Let me know how commit 5f153b56214f works? I don't have a test 
> >> >> environment
> >> >> for this, but it seems to produce the errors right on 32 and 64 bit at
> >> >> least...
> >> >
> >> > it's a bit early in the morning for me, but doesn't the change from
> >> > `unsigned long long` to `unsigned long` mean that by the time you test
> >> > the result it can't be too large because you've already truncated?
> >>
> >> The theory is that by switching strtoull() to strtoul() we get ERANGE when 
> >> it's
> >> truncated.
> >
> > ah, yeah, i see. (a shame though, because i much prefer the "%lx>%d
> > bytes" error message. but i'm unlikely to ever run a 32-bit toybox
> > binary ever again, so my opinion isn't worth much :-) )
>
> You still get that for 1 and 2 bytes, it's just the max size that never makes 
> it
> to the later test if it can't parse from ascii into the long.
>
> This was already the case for 8 bytes on 64 bit. It can't say doesn't fit in 8
> bytes, so it says whatever the strerror() for ERANGE is...
>
> libc/bionic/strerror.cpp:    [ERANGE] = "Math result not representable",
>
> That.
>
> Rob
From 6d32dd2bd3df6eb7c3a216c0ec946824c62618f2 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Tue, 10 Oct 2023 12:12:18 -0700
Subject: [PATCH] devmem: unify error reporting.

---
 toys/other/devmem.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/toys/other/devmem.c b/toys/other/devmem.c
index ce158c2c..43ddacd2 100644
--- a/toys/other/devmem.c
+++ b/toys/other/devmem.c
@@ -17,13 +17,16 @@ config DEVMEM
 #define FOR_devmem
 #include "toys.h"
 
-unsigned long atolu(char *str)
+unsigned long xatolu(char *str, int bytes)
 {
   char *end = str;
   unsigned long lu;
 
   errno = 0;
   lu = strtoul(str, &end, 0);
+  // Report out of range values as errors rather than truncating.
+  if (errno == ERANGE || lu > (~0UL)>>(sizeof(long)-bytes)*8)
+    error_exit("%s>%d bytes", str, bytes);
   if (*end || errno) perror_exit("bad %s", str);
 
   return lu;
@@ -32,7 +35,8 @@ unsigned long atolu(char *str)
 void devmem_main(void)
 {
   int writing = toys.optc == 3, page_size = sysconf(_SC_PAGESIZE), bytes = 4,fd;
-  unsigned long data = 0, map_off, map_len, addr = atolu(*toys.optargs);
+  unsigned long data = 0, map_off, map_len,
+    addr = xatolu(*toys.optargs, sizeof(long));
   char *sizes = sizeof(long)==8 ? "1248" : "124";
   void *map, *p;
 
@@ -45,9 +49,8 @@ void devmem_main(void)
     bytes = 1<<i;
   }
 
-  // DATA? Report out of range values as errors rather than truncating.
-  if (writing && (data = atolu(toys.optargs[2]))>(~0UL)>>(sizeof(long)-bytes)*8)
-    error_exit("%lx>%d bytes", data, bytes);
+  // DATA?
+  if (writing) data = xatolu(toys.optargs[2], bytes);
 
   // Map in just enough.
   if (CFG_TOYBOX_FORK) {
-- 
2.42.0.609.gbb76f46606-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to