android - How to post exception of arithmetic overflow in C/C++ with compiler convenience -
when run android program, find native exception, believe exception comes line of code may cause arithmetic overflow. question arithmetic overflow not report in c/c++, least when test g++ , linux os.
i presume overflow reported because of additional compiler functions when compiling android. exact question how make following code post exceptions when running it.
int main(){ size_t size = 0; size--; return 0; }
size_t
unsigned type. there never arithmetic overflow. instead result of calculations on unsigned types wrapped within value range, if repeatedly adding or subtracting 1 more maximum value. means after size--
, size
hold value size_max
.
signed integer overflow, on other hand, has undefined behaviour. might interested in catching that. gcc has support catching undefined behaviour related signed integer overflow. given following program, undefined behaviour happens when size = int_min
decremented:
#include <stdio.h> #include <limits.h> volatile int size = int_min; int main(){ size--; printf("%d\n", size); return 0; }
compile program
% gcc foo.c -wall -wextra -woverflow -fsanitize=undefined
and no error occurs. if run it, see
% ./a.out foo.c:5:9: runtime error: signed integer overflow: -2147483648 - 1 cannot represented in type 'int' 2147483647
it possible configure abort on first trouble, don't think useful.
Comments
Post a Comment