vpiotr / decimal_for_cpp

Decimal data type for C++
273 stars 68 forks source link

Problem with overflow detection with CLANG compiler.. #32

Closed MagnusEklof closed 6 years ago

MagnusEklof commented 6 years ago

I had a problem with CLANG compiler for ARM. In release build the code below was optimized in a way that the multiplication and division did not occur as separate operations and no overflow was detected (there should have been an overflow). When I turned off optimization it worked as intended. In the VS 2017 compiler this worked as intended in both release and debug.

Original code that was optimized to "true" as in no overflow detected although there was an overflow:

        int64 resDecPart = value1dec * value2dec;
    if (resDecPart / value1dec == value2dec) { // no overflow

I changed the code to this and that works in CLANG and VS compiler, both release and debug

if (value1dec != 0 && (std::numeric_limits<int64>::max() / value1dec) >= value2dec) { // no overflow
vpiotr commented 6 years ago

Hi, thanks for indicating this problem, I have fixed it another way, please check most recent version.