From: Tom Lane <tgl@sss.pgh.pa.us>
To: Victor Petrovykh <victor@magic.io>
Cc: andrew@tao11.riddles.org.uk
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Date: Fri, 23 Nov 2018 20:42:36 -0500
Message-ID: <18151.1543023756@sss.pgh.pa.us> (raw)
In-Reply-To: <CAJ-A5aNZjv+BSg08ZtMwsmiqucKcBeSirfYs16AnY5azD=ZHZw@mail.gmail.com>
References: <15519-4fc785b483201ff1@postgresql.org>
<874lc7sb1a.fsf@news-spur.riddles.org.uk>
<13162.1543014227@sss.pgh.pa.us>
<87zhtzqs9o.fsf@news-spur.riddles.org.uk>
<15115.1543017152@sss.pgh.pa.us>
<87va4nqr1a.fsf@news-spur.riddles.org.uk>
<CAJ-A5aNZjv+BSg08ZtMwsmiqucKcBeSirfYs16AnY5azD=ZHZw@mail.gmail.com>
Victor Petrovykh <victor@magic.io> writes:
> Am I missing something in thinking that the cast of a float to int should
> produce an error if the sign of the original value doesn't match the sign
> of the cast result?
Well, yeah, our traditional way of coding this overflow check would
probably have compared the sign of the result to the sign of the input,
but (a) we'd still have needed some ad-hoc range check to avoid getting
fooled by inputs large enough to have wrapped around an even number of
times, and (b) this approach depends on the compiler not taking any
liberties based on an assumption that the program doesn't provoke
integer overflow. (b) gets more worrisome with each year that goes by,
because the compiler guys keep finding ever-more-creative ways to break
your code if it violates C-spec semantics. So we really want to write
a test that will fail exactly when the integer coercion would overflow,
not do the coercion and then check to see if it overflowed.
regards, tom lane
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: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, victor@magic.io, andrew@tao11.riddles.org.uk, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
In-Reply-To: <18151.1543023756@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox