Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dave Page <dpage(at)postgresql(dot)org>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-committers <pgsql-committers(at)postgresql(dot)org>
Subject: Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64
Date: 2011-04-28 20:30:10
Message-ID: 12933.1304022610@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

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

In response to

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Andrew Dunstan 2011-04-29 00:03:11 pgsql: Use non-literal format for possibly non-standard strftime format
Previous Message Dave Page 2011-04-28 20:11:21 Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64

Browse pgsql-hackers by date

  From Date Subject
Next Message Josh Berkus 2011-04-28 20:54:05 Re: Extreme bloating of intarray GiST indexes
Previous Message Dave Page 2011-04-28 20:11:21 Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64