sfu-dis / pibench

Benchmarking framework for index structures on persistent memory
MIT License
87 stars 20 forks source link

Signed to unsigned implicit conversion causes overflow in latency sampling #36

Closed dpsi closed 3 years ago

dpsi commented 3 years ago

Undefined behaviour sanitizer error: runtime error: implicit conversion from type 'int' of value -2147031364 (32-bit, signed) to type 'uint64_t' (aka 'unsigned long') changed the value to 18446744071562520252 (64-bit, unsigned)

The prototype of std::accumulate is:

template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
              BinaryOperation op );

and the type T is deduced from the parameter init which is 0 aka signed int. The op lambda can return a value larger than 2147483647 which is implicitly converted back to the init type as a negative value. The next application of op is given a negative signed int as the sum which is implicitly converted to a very large 64bit number.