These additions might overflow the signed range for large input values. Converting to unsigned before the addition rather than after avoids such undefined behaviour. The result under normal two's complement wraparound remains unchanged.
Showing the changes to libavutil/intfloat_readwrite.c (1 of 1 file). Show all
Unified · Split
| @@ -30,13 +30,13 @@ · 29 unmodified lines | ||
| 30 | 30 | #include "intfloat_readwrite.h" |
| 31 | 31 | |
| 32 | 32 | double av_int2dbl(int64_t v){ |
| 33 | − if(v+v > 0xFFEULL<<52) | |
| 33 | + if((uint64_t)v+v > 0xFFEULL<<52) | |
| 34 | 34 | return NAN; |
| 35 | 35 | return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | float av_int2flt(int32_t v){ |
| 39 | − if(v+v > 0xFF000000U) | |
| 39 | + if((uint32_t)v+v > 0xFF000000U) | |
| 40 | 40 | return NAN; |
| 41 | 41 | return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150); |
| 42 | 42 | } |