]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Fix printing of INT(MAX|64)_MIN. In append_int we had theoretically
authorColin Percival <cperciva@daemonology.net>
Tue, 2 Aug 2011 12:20:40 +0000 (08:20 -0400)
committerColin Percival <cperciva@daemonology.net>
Tue, 2 Aug 2011 12:20:40 +0000 (08:20 -0400)
undefined behaviour, but the code probably worked on all existing
compilers and platforms.  In format_int we could get the wrong output,
but only on files created/modified 280 billion years before the Big
Bang.

Reported by: Ralph Corderoy (via Tarsnap)

SVN-Revision: 3514

libarchive/archive_string_sprintf.c
libarchive/archive_write_set_format_pax.c

index 4e824dceeebec382dc8f77258abe76c8afc22129..12cd5e042435ac944b13252010e75fd2eaf44f8d 100644 (file)
@@ -60,11 +60,14 @@ append_uint(struct archive_string *as, uintmax_t d, unsigned base)
 static void
 append_int(struct archive_string *as, intmax_t d, unsigned base)
 {
+       uintmax_t ud;
+
        if (d < 0) {
                archive_strappend_char(as, '-');
-               d = -d;
-       }
-       append_uint(as, d, base);
+               ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : -d;
+       } else
+               ud = d;
+       append_uint(as, ud, base);
 }
 
 
index c5a3005719553ca5e898f46fcd3038cca6d29fe4..20743f986c095203f22a2946398f026af4accbb5 100644 (file)
@@ -240,18 +240,17 @@ add_pax_attr_time(struct archive_string *as, const char *key,
 static char *
 format_int(char *t, int64_t i)
 {
-       int sign;
+       uint64_t ui;
 
-       if (i < 0) {
-               sign = -1;
-               i = -i;
-       } else
-               sign = 1;
+       if (i < 0) 
+               ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : -i;
+       else
+               ui = i;
 
        do {
-               *--t = "0123456789"[i % 10];
-       } while (i /= 10);
-       if (sign < 0)
+               *--t = "0123456789"[ui % 10];
+       } while (ui /= 10);
+       if (i < 0)
                *--t = '-';
        return (t);
 }