From: Colin Percival Date: Tue, 2 Aug 2011 12:20:40 +0000 (-0400) Subject: Fix printing of INT(MAX|64)_MIN. In append_int we had theoretically X-Git-Tag: v3.0.0a~217 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa12bb431326f494ddb24a180ec4e06f5b454a27;p=thirdparty%2Flibarchive.git Fix printing of INT(MAX|64)_MIN. In append_int we had theoretically 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 --- diff --git a/libarchive/archive_string_sprintf.c b/libarchive/archive_string_sprintf.c index 4e824dcee..12cd5e042 100644 --- a/libarchive/archive_string_sprintf.c +++ b/libarchive/archive_string_sprintf.c @@ -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); } diff --git a/libarchive/archive_write_set_format_pax.c b/libarchive/archive_write_set_format_pax.c index c5a300571..20743f986 100644 --- a/libarchive/archive_write_set_format_pax.c +++ b/libarchive/archive_write_set_format_pax.c @@ -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); }