Running make -C gcc check RUNTESTFLAGS='--target_board=unix/-fsanitize=undefined dg.exp=*const-expr*' unveiled a pretty stupid bug: we should instrument expression only when it's actually needed, not otherwise. On the C++ FE this is handled right, the thinko was only in C FE.
Tested x86_64-unknown-linux-gnu, applying to ubsan branch. diff --git a/gcc/c/ChangeLog.ubsan b/gcc/c/ChangeLog.ubsan index 11d167f..f41ae90 100644 --- a/gcc/c/ChangeLog.ubsan +++ b/gcc/c/ChangeLog.ubsan @@ -1,3 +1,8 @@ +2013-07-31 Marek Polacek <pola...@redhat.com> + + * c-typeck.c (build_binary_op): Sanitize only when + doing shift or division. + 2013-07-30 Marek Polacek <pola...@redhat.com> * c-typeck.c (build_binary_op): Sanitize only when diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 7257166..2595382 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -10489,7 +10489,8 @@ build_binary_op (location_t location, enum tree_code code, } if (flag_sanitize & SANITIZE_UNDEFINED - && current_function_decl != 0) + && current_function_decl != 0 + && (doing_div_or_mod || doing_shift)) { /* OP0 and/or OP1 might have side-effects. */ op0 = c_save_expr (op0); Marek