[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: catching 32/64 bit integer mixing



* Drew Parsons:

> An upstream author has asked whether we know of tools or compiler flags 
> to help catch problems mixing 64 and 32 bit integers, for instance 
> catching implicit conversions, as in
>
>    int64_t n = ...;
>    for (int32_t i=0; i<n; i++) {
>      ...
>    }
>
> There is -fsanitize=signed-integer-overflow (which generates a runtime 
> error message), but that requires data to cause overflow at every 
> possible location.
>
> Is there a better tool for catching this kind of problem?

The snippet above is really hard to diagnose because there is no
conversion from 64 bits to 32 bits involved.  For more direct cases,
there is -Wconversion and -Wshorten-64-to-32 (the latter may be
Clang-only).

There is a lot of C++ code out there which uses int for std::vector
subscripts, usually in a harmless way (but not always), so warnings
may not be that useful after all.


Reply to: