diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c index 6b64a85..408fd77 100644 --- a/src/backend/utils/adt/numutils.c +++ b/src/backend/utils/adt/numutils.c @@ -339,43 +339,31 @@ pg_ltostr(char *str, int32 value) char *end; /* - * Handle negative numbers in a special way. We can't just append a '-' - * prefix and reverse the sign as on two's complement machines negative - * numbers can be 1 further from 0 than positive numbers, we do it this way - * so we properly handle the smallest possible value. + * Avoid problems with the most negative integer not being representable + * as a positive integer. */ - if (value < 0) + if (value == (-2147483647 - 1)) { + memcpy(str, "-2147483648", 11); + return str + 11; + } + else if (value < 0) + { + value = -value; *str++ = '-'; - - /* mark the position we must reverse the string from. */ - start = str; - - /* Compute the result string backwards. */ - do - { - int32 remainder; - int32 oldval = value; - - value /= 10; - remainder = oldval - value * 10; - *str++ = '0' + -remainder; - } while (value != 0); } - else + start = str; + + /* Compute the result string backwards. */ + do { - /* mark the position we must reverse the string from. */ - start = str; - do - { - int32 remainder; - int32 oldval = value; + int32 remainder; + int32 oldval = value; - value /= 10; - remainder = oldval - value * 10; - *str++ = '0' + remainder; - } while (value != 0); - } + value /= 10; + remainder = oldval - value * 10; + *str++ = '0' + remainder; + } while (value != 0); /* Remember the end of string and back up 'str' to the last character. */ end = str--;