diff --git a/src/ChangeLog b/src/ChangeLog index 09d4bc70..f54dd7f4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2002-04-14 Hrvoje Niksic + + * utils.c (number_to_string): Handle the case when n < -INT_MAX. + 2002-04-14 Hrvoje Niksic * init.c (comind): Use a marginally faster implementation of diff --git a/src/utils.c b/src/utils.c index 33514493..a84237ee 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1347,6 +1347,12 @@ numdigit (long number) return cnt; } +/* A half-assed implementation of INT_MAX on machines that don't + bother to define one. */ +#ifndef INT_MAX +# define INT_MAX ((int) ~((unsigned)1 << 8 * sizeof (int) - 1)) +#endif + #define ONE_DIGIT(figure) *p++ = n / (figure) + '0' #define ONE_DIGIT_ADVANCE(figure) (ONE_DIGIT (figure), n %= (figure)) @@ -1406,6 +1412,15 @@ number_to_string (char *buffer, long number) if (n < 0) { + if (n < -INT_MAX) + { + /* We cannot print a '-' and assign -n to n because -n would + overflow. Let sprintf deal with this border case. */ + sprintf (buffer, "%ld", n); + p += strlen (buffer); + return p; + } + *p++ = '-'; n = -n; }