Re: overflow in snprintf() when printing INT64_MIN

From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: overflow in snprintf() when printing INT64_MIN
Date: 2018-09-28 00:44:59
Message-ID: 20180928004459.266cwsr3vlv3pohj@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2018-09-27 17:34:54 -0700, Andres Freund wrote:
> On 2018-09-27 20:18:12 -0400, Tom Lane wrote:
> > > /* Handle +/- */
> > > if (dosign && adjust_sign((value < 0), forcesign, &signvalue);
> > > uvalue = -(uint64) value;
> > > else
> > > uvalue = (uint64) value;
> >
> > Hm, what does -x mean for an unsigned value? I'm not really
> > convinced this is conceptually better.
>
> 6.2.5 (9): "... A computation involving unsigned operands can never
> overflow, because a result that cannot be represented by the resulting
> unsigned integer type is reduced modulo the number that is one greater
> than the largest value that can be represented by the resulting type."
>
> (unsigned)((int)-1) == 4294967295
> -(unsigned)4294967295 == 1
>
> I think that's well defined.

I guess some might consider
uvalue = (uint64) 0 - (uint64) value
to be easier to reason about?

Greetings,

Andres Freund

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2018-09-28 01:21:16 Re: Postgres 11 release notes
Previous Message Andres Freund 2018-09-28 00:34:54 Re: overflow in snprintf() when printing INT64_MIN