1
Vote

Fix Run-Time Check Failure

description

It's kind of ironic to have a Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of data.
Am I the only one that enables all runtime checks in Visual Studio?

I have made the following change to fix it:
template < typename E >
static void RegMultiplyThrow( const unsigned __int64& a, const unsigned __int64& b, unsigned __int64* pRet ) SAFEINT_CPP_THROW
{
[...]
        unsigned __int32 aHigh, aLow, bHigh, bLow;

        static const unsigned __int64 mask = 0x00000000FFffFFff;

        aHigh = (unsigned __int32)(a >> 32);
        //aLow  = (unsigned __int32)a;
        aLow  = (unsigned __int32)(a & mask);
        bHigh = (unsigned __int32)(b >> 32);
        //bLow  = (unsigned __int32)b;
        bLow  = (unsigned __int32)(b & mask);
Question is: why wasn't this (or some more portable alternative) already there?

comments

dcleblanc wrote Mar 14, 2015 at 2:18 AM

Apparently, you are the first one. I didn't immediately find the option to enable these checks in VS 2013.

As to portable, we compiled the code with clang, gcc, and the Microsoft compiler with every warning enabled that we could realistically turn on (/Wall, everything). Except to the extent that casts themselves can be implementation dependent, I believe the code above is very standards compliant.

The code is correct, complies with the C++ standard, and it is very portable (except not to processors that don't use 2's complement negative numbers, but these are very rare).

I don't mind making the fix, but I'd be surprised if there is very much software that runs very long with those checks. Have you by any chance taken a look at the assembly emitted?