cginternals / libzeug

deprecated: C++ sanctuary for small but powerful and frequently required, stand alone features.
MIT License
16 stars 13 forks source link

Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of data. #153

Open j-o opened 8 years ago

j-o commented 8 years ago

Caused by duktape (1.4 as well). Is there a way to remove the compiler option /RTCc for the target scriptzeug only, leaving it on in the DEFAULT_COMPILE_OPTIONS?

sbusch42 commented 8 years ago

At the moment, we made the mistake of putting DEFAULT_COMPILE_OPTIONS in PUBLIC instead of PRIVATE, so even if we disable it for a specific project, it will be inherited from the dependencies, e.g., scriptzeug depends on reflectionzeug.

Regarding the problem itself: We do not yet know the real cause of the problem, but I doubt only duktape is affected, the same error occurs also in other parts of the middleware. So I would vote for completely removing RTCc, unless we find out what the real problem is here.

@cgcostume Any ideas on this matter?

scheibel commented 8 years ago

I remember from former occurrences of this runtime warning that it gets yielded in strange situations with somehow well-written and well-structured code. As a solution, the code has to get much more complicated. Example:

int value = 42;
unsigned char result = value & 255; // result is always be between 0 and 255 and thus fits perfectly into an unsigned char

The code above may result in such runtime warning and has to be rewritten as this:

int value = 42;
unsigned char result = static_cast<unsigned char>(value & 255);

I would also vote for disabling RTCc.

j-o commented 8 years ago

The full error message is

Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of data.  If this was intentional, you should mask the source of the cast with the appropriate bitmask.  For example:  

    char c = (i & 0xFF);

Changing the code in this way will not affect the quality of the resulting optimized code.

I would understand that is is more about adding that mask when doing a cast, so both of your examples should be fine. The offending line in this issue is

res->hash_seed = (duk_uint32_t) (duk_intptr_t) res;

i.e., a cast from a 64 bit int to a 32 bit int with actual data loss. There might be some more experiments necessary, but I think this warning is appropriate for the 64 to 32 bit cast.

cgcostume commented 8 years ago

I disabled RTCc for all our projects (libzeug only in update_duktape branch) for now. If the problem resides in duktape i suggest to report an issue or fix it right away and, if resolved, enable RTCc in our projects again.