Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64 - Mailing list pgsql-committers

From Tom Lane
Subject Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64
Date
Msg-id 12933.1304022610@sss.pgh.pa.us
Whole thread Raw
In response to Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64  (Dave Page <dpage@postgresql.org>)
Responses Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64  (Dave Page <dpage@postgresql.org>)
List pgsql-committers
Please see if the attached version works.

            regards, tom lane


Datum
pg_size_pretty(PG_FUNCTION_ARGS)
{
    int64        size = PG_GETARG_INT64(0);
    char        buf[64];
    int64        limit = 10 * 1024;
    int64        limit2 = limit * 2 - 1;

    if (size < limit)
        snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size);
    else
    {
        size >>= 9;                /* keep one extra bit for rounding */
        if (size < limit2)
            snprintf(buf, sizeof(buf), INT64_FORMAT " kB",
                     (size + 1) / 2);
        else
        {
            size >>= 10;
            if (size < limit2)
                snprintf(buf, sizeof(buf), INT64_FORMAT " MB",
                         (size + 1) / 2);
            else
            {
                size >>= 10;
                if (size < limit2)
                    snprintf(buf, sizeof(buf), INT64_FORMAT " GB",
                             (size + 1) / 2);
                else
                {
                    size >>= 10;
                    snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
                             (size + 1) / 2);
                }
            }
        }
    }

    PG_RETURN_TEXT_P(cstring_to_text(buf));
}

pgsql-committers by date:

Previous
From: Dave Page
Date:
Subject: Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64
Next
From: Andrew Dunstan
Date:
Subject: pgsql: Use non-literal format for possibly non-standard strftime format