Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Date
Msg-id 18151.1543023756@sss.pgh.pa.us
Whole thread Raw
In response to BUG #15519: Casting float4 into int4 gets the wrong sign instead of"integer out of range" error  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #15519: Casting float4 into int4 gets the wrong sign insteadof "integer out of range" error  (Victor Petrovykh <victor@magic.io>)
List pgsql-bugs
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


pgsql-bugs by date:

Previous
From: Victor Petrovykh
Date:
Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign insteadof "integer out of range" error
Next
From: PG Bug reporting form
Date:
Subject: BUG #15520: PAM authentication + domain socket -> DNS query forsymbolic hostname [local]