2017-06-21 Jakub Jelinek PR sanitizer/80998 * fold-const.c: Include asan.h. (build_range_check): For -fsanitize=pointer-overflow don't add pointer arithmetics for range test. --- gcc/fold-const.c.jj 2017-06-14 18:07:47.000000000 +0200 +++ gcc/fold-const.c 2017-06-20 17:05:44.351608513 +0200 @@ -79,6 +79,7 @@ along with GCC; see the file COPYING3. #include "tree-vrp.h" #include "tree-ssanames.h" #include "selftest.h" +#include "asan.h" /* Nonzero if we are folding constants inside an initializer; zero otherwise. */ @@ -4906,6 +4907,14 @@ build_range_check (location_t loc, tree { if (value != 0 && !TREE_OVERFLOW (value)) { + /* Avoid creating pointer arithmetics that is not present + in the source when sanitizing. */ + if (!integer_zerop (low) + && current_function_decl + && sanitize_flags_p (SANITIZE_POINTER_OVERFLOW, + current_function_decl)) + return 0; + low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low); return build_range_check (loc, type, fold_build_pointer_plus_loc (loc, exp, low),