This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix undefined float behaviour in pack('f')
authorDavid Mitchell <davem@iabyn.com>
Mon, 22 Dec 2014 09:34:40 +0000 (09:34 +0000)
committerDavid Mitchell <davem@iabyn.com>
Wed, 31 Dec 2014 11:28:51 +0000 (11:28 +0000)
commit919894b775f42c5afd2195b1a5240e66205560d1
tree32a0976a5df5f608512c5840bdf109ac299c8039
parent2afa5dd07629c20c6931b8c5e4c1671520787a9d
fix undefined float behaviour in pack('f')

The C standard says that the value of the expression (float)double_var is
undefined if 'the value being converted is outside the range of values
that can be represented'.

So to shut up -fsanitize=undefined:

    my $p = pack 'f', 1.36514538e67;

giving

    runtime error: value 1.36515e+67 is outside the range of representable values of type 'float'

explicitly handle the out of range values.
Something similar is already done under defined(VMS) && !defined(_IEEE_FP),
except that there it floors to +/- FLT_MAX rather than +/- (float)NV_INF.
I don't know which branch is best, and whether they should be merged.

This fix was suggested by Aaron Crane.
pp_pack.c