diff options
author | David Rowley <drowley@postgresql.org> | 2024-07-28 22:23:32 +1200 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-07-28 22:23:32 +1200 |
commit | 1e020258e53c87c697615390c42a191344bbb909 (patch) | |
tree | f7cc73d6b637183e3ca765444923f8dd3f94ec54 /src/backend/utils/adt/dbsize.c | |
parent | 821fbd63eab0c35c034e83d278da7244cf1be463 (diff) | |
download | postgresql-1e020258e53c87c697615390c42a191344bbb909.tar.gz postgresql-1e020258e53c87c697615390c42a191344bbb909.zip |
Fix incorrect return value for pg_size_pretty(bigint)
pg_size_pretty(bigint) would return the value in bytes rather than PB
for the smallest-most bigint value. This happened due to an incorrect
assumption that the absolute value of -9223372036854775808 could be
stored inside a signed 64-bit type.
Here we fix that by instead storing that value in an unsigned 64-bit type.
This bug does exist in versions prior to 15 but the code there is
sufficiently different and the bug seems sufficiently non-critical that
it does not seem worth risking backpatching further.
Author: Joseph Koshakow <koshy44@gmail.com>
Discussion: https://postgr.es/m/CAAvxfHdTsMZPWEHUrZ=h3cky9Ccc3Mtx2whUHygY+ABP-mCmUw@mail.gmail.com
Backpatch-through: 15
Diffstat (limited to 'src/backend/utils/adt/dbsize.c')
-rw-r--r-- | src/backend/utils/adt/dbsize.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index 25d7110c130..b2d9cc27929 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -575,9 +575,13 @@ pg_size_pretty(PG_FUNCTION_ARGS) for (unit = size_pretty_units; unit->name != NULL; unit++) { uint8 bits; + uint64 abs_size = size < 0 ? 0 - (uint64) size : (uint64) size; - /* use this unit if there are no more units or we're below the limit */ - if (unit[1].name == NULL || i64abs(size) < unit->limit) + /* + * Use this unit if there are no more units or the absolute size is + * below the limit for the current unit. + */ + if (unit[1].name == NULL || abs_size < unit->limit) { if (unit->round) size = half_rounded(size); |