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
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);
}
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);
}