public inbox for [email protected]  
help / color / mirror / Atom feed
From: Joseph Koshakow <[email protected]>
To: David Rowley <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: Fix overflow in pg_size_pretty
Date: Sat, 27 Jul 2024 19:06:30 -0400
Message-ID: <CAAvxfHce9wPU7w-SKTmBQ0EYTCsFwUNVxCRTYaCY2SftSq5EyQ@mail.gmail.com> (raw)
In-Reply-To: <CAApHDvr421-9UK+BKfwKyZxn5XhbTLOJXppnAgkPqpCJQcoCCQ@mail.gmail.com>
References: <CAAvxfHdTsMZPWEHUrZ=h3cky9Ccc3Mtx2whUHygY+ABP-mCmUw@mail.gmail.com>
	<CAApHDvr421-9UK+BKfwKyZxn5XhbTLOJXppnAgkPqpCJQcoCCQ@mail.gmail.com>

On Sat, Jul 27, 2024 at 6:28 PM David Rowley <[email protected]> wrote:
>
> On Sun, 28 Jul 2024 at 07:18, Joseph Koshakow <[email protected]> wrote:
>> Attached is a patch that resolves an overflow in pg_size_pretty() that
>> resulted in unexpected behavior when PG_INT64_MIN was passed in as an
>> argument.
>
> Could we just fix this more simply by assigning the absolute value of
> the signed variable into an unsigned type?

I might be misunderstanding, but my previous patch does assign the
absolute value of the signed variable into an unsigned type.

> It's a bit less code and
> gets rid of the explicit test for PG_INT64_MIN.

> + uint64 usize = size < 0 ? (uint64) (-size) : (uint64) size;

I think that the explicit test for PG_INT64_MIN is still required. If
`size` is equal to PG_INT64_MIN then `-size` will overflow. You end up
with the correct behavior if `size` wraps around, but that's only
guaranteed on platforms that support the `-fwrapv` flag.

Thanks,
Joseph Koshakow


view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Fix overflow in pg_size_pretty
  In-Reply-To: <CAAvxfHce9wPU7w-SKTmBQ0EYTCsFwUNVxCRTYaCY2SftSq5EyQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox