Skip to content

Commit 6ba0d8d

Browse files
committed
Fix pg_size_pretty() to avoid overflow for inputs close to INT64_MAX.
The expression that tried to round the value to the nearest TB could overflow, leading to bogus output as reported in bug #5993 from Nicola Cossu. This isn't likely to ever happen in the intended usage of the function (if it could, we'd be needing to use a wider datatype instead); but it's not hard to give the expected output, so let's do so.
1 parent 147d626 commit 6ba0d8d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/backend/utils/adt/dbsize.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,15 @@ pg_size_pretty(PG_FUNCTION_ARGS)
497497
(size + mult / 2) / mult);
498498
else
499499
{
500+
/* Here we have to worry about avoiding overflow */
501+
int64 val;
502+
500503
mult *= 1024;
504+
val = size / mult;
505+
if ((size % mult) >= (mult / 2))
506+
val++;
501507
snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
502-
(size + mult / 2) / mult);
508+
val);
503509
}
504510
}
505511
}

0 commit comments

Comments
 (0)