BIC-MNI / libminc

libminc is the core library and API of the MINC toolkit
Other
19 stars 29 forks source link

Undefined behaviour caught by UBSan in "minc2-hyper-test-2" test #36

Closed seanm closed 9 years ago

seanm commented 9 years ago

UBSan finds only one test failure (nice!):

The test "minc2-hyper-test-2" has this loop:

   59     for ( i = 0; i < CZ * CY * CX; i++ ) {
-> 60       buf[i] = ( unsigned short ) (i * 0.01);
   61     }

Also note:

#define CZ 142
#define CY 245
#define CX 210

Thus the loop runs 7 305 900 times, which even after dividing by 100 gives numbers too big to fit in an unsigned 16 bit number and UBSan warns:

41: (/Users/builder/external/libminc-clang-dbg-x86_64-static/testdir/minc2-hyper-test-2+0x100004e36): runtime error: value 65536 is outside the range of representable values of type 'unsigned short'

Does the buffer contents matter much? If I divide by 1000 instead the test passed and the warning is gone.

rdvincent commented 9 years ago

It is non-critical, as I don't think that test actually checks the values of data it reads.

seanm commented 9 years ago

So you'd accept a patch to do * 0.001 ?

rdvincent commented 9 years ago

Yes.

seanm commented 9 years ago

https://github.com/BIC-MNI/libminc/pull/38