tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
330 stars 97 forks source link

`READ_DATA` macro needs checks before float to int casts #241

Open seanm opened 4 months ago

seanm commented 4 months ago

Hi @tbeu,

I've been fuzzing lately, and found something that's better to discuss before I attempt any PR. The macro READ_DATA has this line:

data[i + j] = (T)SwapFunc(&v[j]);

When v is float/double it could be outside the range of the integer type T. Like for huge or non-finite floats. UBSan will then complain of undefined behaviour casting such things to integers.

Probably some range checks will be needed, but that may be tricky as this macro works for many different types...

tbeu commented 4 months ago

One idea would be to additionally pass min and max rage values to macros READ_DATA and READ_COMPRESSED_DATA and check if in range before casting.