overflow in snprintf() when printing INT64_MIN

Поиск
Список
Период
Сортировка
От Andres Freund
Тема overflow in snprintf() when printing INT64_MIN
Дата
Msg-id 20180928001121.hhx5n6dsygqxr5wu@alap3.anarazel.de
обсуждение исходный текст
Ответы Re: overflow in snprintf() when printing INT64_MIN  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-hackers
Hi,

I just noticed, while reviewing a patch that corrects overflow handing
in snprintf, that we don't correctly handle INT64_MIN in snprintf.c:

static void
fmtint(int64 value, char type, int forcesign, int leftjust,
           int minlen, int zpad, int precision, int pointflag,
           PrintfTarget *target)
{
...
        /* Handle +/- */
        if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
                value = -value;

If value already is INT64_MIN this can't work.  It just happens to fail
to fail, because the later cast with (uint64) value "hides" the damage.

I suspect the best way to fix this, would be to instead do:

    /* Handle +/- */
    if (dosign && adjust_sign((value < 0), forcesign, &signvalue);
        uvalue = -(uint64) value;
    else
        uvalue = (uint64) value;

Greetings,

Andres Freund


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Andres Freund
Дата:
Сообщение: Re: [HACKERS] kqueue
Следующее
От: Tom Lane
Дата:
Сообщение: Re: overflow in snprintf() when printing INT64_MIN