time_t on 32-bit Android is 32 bits.

that particular value looks like a sign-extension of 0xAFBEADCE, which
is still some time in 2063. (so i'd assume this device's clock is set
wrong, and i'd assume -- since this is presumably a 32-bit device with
a signed 32-bit time_t -- that that's going to be causing other
problems too.)

https://android.googlesource.com/platform/external/toybox/+/android-6.0.1_r77/toys/other/stat.c
doesn't look to different from today.

with 64-bit toybox:

angler:/ # date 060200002064
Mon Jun  2 00:00:00 GMT 2064
angler:/ # rm /data/local/tmp/empty
angler:/ # touch /data/local/tmp/empty
angler:/ # ls -l /data/local/tmp/empty
-rw-rw-rw- 1 root root 0 2064-06-02 00:00 /data/local/tmp/empty
angler:/ # stat /data/local/tmp/empty
  File: `/data/local/tmp/empty'
  Size: 0 Blocks: 0 IO Blocks: 512 regular empty file
Device: fd00h/64768d Inode: 3014659 Links: 1
Access: (666/-rw-rw-rw-) Uid: (    0/    root) Gid: (    0/    root)
Access: 2064-06-02 00:00:11.746667836
Modify: 2064-06-02 00:00:11.746667836
Change: 2064-06-02 00:00:11.746667836
angler:/ # stat -c "%z %Z" /data/local/tmp/empty
2064-06-02 00:00:11.746667836 2979590411
angler:/ # ^D

then with 32-bit toybox:

angler:/ # ls -l /data/local/tmp/empty
-rw-rw-rw- 1 root root 0 1928-04-26 17:31 /data/local/tmp/empty
angler:/ # stat /data/local/tmp/empty
  File: `/data/local/tmp/empty'
  Size: 0 Blocks: 0 IO Blocks: 512 regular empty file
Device: fd00h/64768d Inode: 3014659 Links: 1
Access: (666/-rw-rw-rw-) Uid: (    0/    root) Gid: (    0/    root)
Access: 1928-04-26 17:31:55.746667836
Modify: 1928-04-26 17:31:55.746667836
Change: 1928-04-26 17:31:55.746667836
angler:/ # stat -c "%z %Z" /data/local/tmp/empty
1928-04-26 17:31:55.746667836 18446744072394174731

two patches attached. one avoids sign extension for all calls to
`out`, fixing %Z for systems with a signed 32-bit time_t. the other
removes void* casts unnecessary since POSIX 2008 and fixes the
strftime buffer length argument.

%z is still broken for systems with signed 32-bit time_t, but i think
that's just inherent on such systems.

On Fri, Dec 30, 2016 at 9:28 AM, darken <dar...@darken.eu> wrote:
> I've seen a value of "18446744072363093454" for stat %Z (seconds since
> epoch), for some files on a users device (Android 6.01).
>
> This seems suspiciously large and I'm wondering what the valid range for
> this value is.
> What range is valid for the filesystem to return and what value range can
> toybox handle?
>
> ~Matthias
>
> _______________________________________________
> Toybox mailing list
> Toybox@lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
From be5dd0c57df9d35bff7671c897e0b2dd384bdb84 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Fri, 30 Dec 2016 11:24:25 -0800
Subject: [PATCH] Avoid sign extension in stat.c.

---
 toys/other/stat.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/toys/other/stat.c b/toys/other/stat.c
index 9d64579..63dfba8 100644
--- a/toys/other/stat.c
+++ b/toys/other/stat.c
@@ -50,9 +50,19 @@ GLOBALS(
   int patlen;
 )
 
-// Force numeric output to long long instead of manually typecasting everything
-// and safely parse length prefix
-static void out(char c, long long val)
+// Force numeric output to unsigned long long instead of manually typecasting
+// everything. Takes unsigned because no struct stat field can be negative,
+// and in combination with the add_unsigned macro this lets us avoid
+// accidental sign extension of signed fields (time_t on LP32, say).
+// We also handle length prefixes here rather than repeating that everywhere.
+#define out(out_c, out_val) uout(out_c, add_unsigned(out_val))
+#define add_unsigned(x) \
+  (sizeof(x) == sizeof(char) ? (unsigned char)(x) : \
+   sizeof(x) == sizeof(short) ? (unsigned short)(x) : \
+   sizeof(x) == sizeof(int) ? (unsigned int)(x) : \
+   sizeof(x) == sizeof(long) ? (unsigned long)(x) : \
+   (unsigned long long)(x))
+static void uout(char c, unsigned long long val)
 {
   sprintf(toybuf, "%.*sll%c", TT.patlen, TT.pattern, c);
   printf(toybuf, val);
-- 
2.8.0.rc3.226.g39d4020

From 1c4a7a8ac7ff68e07df91f5822dd8145229e4bf9 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Fri, 30 Dec 2016 11:19:08 -0800
Subject: [PATCH] Remove unnecessary casts in stat.c, fix a claimed buffer
 length.

POSIX does have a name for the struct timespec in struct stat.
---
 toys/other/stat.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/toys/other/stat.c b/toys/other/stat.c
index b998a0e..9d64579 100644
--- a/toys/other/stat.c
+++ b/toys/other/stat.c
@@ -65,14 +65,10 @@ static void strout(char *val)
   printf(toybuf, val);
 }
 
-// Note: the atime, mtime, and ctime fields in struct stat are the start
-// of embedded struct timespec, but posix won't let them use that
-// struct definition for legacy/namespace reasons.
-
 static void date_stat_format(struct timespec *ts)
 {
   char *s = toybuf+128;
-  strftime(s, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
+  strftime(s, sizeof(toybuf)-128, "%Y-%m-%d %H:%M:%S",
     localtime(&(ts->tv_sec)));
   sprintf(s+strlen(s), ".%09ld", ts->tv_nsec);
   strout(s);
@@ -126,11 +122,11 @@ static void print_stat(char type)
   else if (type == 'T') out('x', dev_minor(stat->st_rdev));
   else if (type == 'u') out('u', stat->st_uid);
   else if (type == 'U') strout(getusername(stat->st_uid));
-  else if (type == 'x') date_stat_format((void *)&stat->st_atime);
+  else if (type == 'x') date_stat_format(&stat->st_atim);
   else if (type == 'X') out('u', stat->st_atime);
-  else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
+  else if (type == 'y') date_stat_format(&stat->st_mtim);
   else if (type == 'Y') out('u', stat->st_mtime);
-  else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
+  else if (type == 'z') date_stat_format(&stat->st_ctim);
   else if (type == 'Z') out('u', stat->st_ctime);
   else xprintf("?");
 }
-- 
2.8.0.rc3.226.g39d4020

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

Reply via email to