ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

noFrac64 and noFrac32 are incorrect for 0. #364

Closed csilvers closed 3 years ago

csilvers commented 3 years ago

In f4b40f6a096194 code was added to allow reading a (non-fraction-containing) float64 into a uint64 struct. This code depends on a new function noFrac64 to decide if the cast is legal. (There is also a noFrac32.) However, noFrac64 and noFrac32 do not return the correct result when the input is 0.0. In that case, subtracting the bias yields a negative number (which is then interpreted as a very large positive number).

The end result is that, in decoding, you can see a lot of errors like this:

assigning integer value from float64 with a fraction: 0

It should resolve this bug to just add if fbits == 0 { return true } to the beginning of noFrac32 and noFrac64.

csilvers commented 3 years ago

It occurs to me that the noFrac code is probably also wrong for NaN and Inf as well.