Integer overflow in C: standards and compilers

前端 未结 7 1156
予麋鹿
予麋鹿 2020-12-01 11:56

Edited to include proper standard reference thanks to Carl Norum.

The C standard states

If an exceptional condition occurs d

7条回答
  •  清歌不尽
    2020-12-01 13:01

    For completeness, I'd like to add that Clang now has "checked arithmetic builtins" as a language extension. Here is an example using checked unsigned multiplication:

    unsigned x, y, result;
    ...
    if (__builtin_umul_overflow(x, y, &result)) {
        /* overflow occured */
        ...
    }
    ...
    

    http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins

提交回复
热议问题