This was meant only as a TLC, but it also fixes PR59306. Using walk_gimple_op was an overkill; gimple_{store,assign_load}_p is enough. As a side effect, it also fixes the bug because now we better restrict what goes into instrument_member_call.
Bootstrapped, ran ubsan testsuite on x86_64-linux, ok for trunk? 2013-11-27 Marek Polacek <pola...@redhat.com> PR sanitizer/59306 * ubsan.c (instrument_null): Use gimple_store_p/gimple_assign_load_p instead of walk_gimple_op. (ubsan_pass): Adjust. Call instrument_null only if SANITIZE_NULL. testsuite/ * g++.dg/ubsan/pr59306.C: --- gcc/ubsan.c.mp3 2013-11-27 09:55:23.573252894 +0100 +++ gcc/ubsan.c 2013-11-27 11:15:29.893507768 +0100 @@ -614,24 +614,22 @@ instrument_mem_ref (tree t, gimple_stmt_ gsi_insert_before (iter, g, GSI_SAME_STMT); } -/* Callback function for the pointer instrumentation. */ +/* Perform the pointer instrumentation. */ -static tree -instrument_null (tree *tp, int * /*walk_subtree*/, void *data) +static void +instrument_null (gimple_stmt_iterator gsi, bool is_lhs) { - tree t = *tp; + gimple stmt = gsi_stmt (gsi); + tree t = is_lhs ? gimple_get_lhs (stmt) : gimple_assign_rhs1 (stmt); + t = get_base_address (t); const enum tree_code code = TREE_CODE (t); - struct walk_stmt_info *wi = (struct walk_stmt_info *) data; - if (code == MEM_REF && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME) - instrument_mem_ref (TREE_OPERAND (t, 0), &wi->gsi, wi->is_lhs); + instrument_mem_ref (TREE_OPERAND (t, 0), &gsi, is_lhs); else if (code == ADDR_EXPR && POINTER_TYPE_P (TREE_TYPE (t)) && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == METHOD_TYPE) - instrument_member_call (&wi->gsi); - - return NULL_TREE; + instrument_member_call (&gsi); } /* Gate and execute functions for ubsan pass. */ @@ -646,7 +644,6 @@ ubsan_pass (void) { for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) { - struct walk_stmt_info wi; gimple stmt = gsi_stmt (gsi); if (is_gimple_debug (stmt) || gimple_clobber_p (stmt)) { @@ -654,9 +651,14 @@ ubsan_pass (void) continue; } - memset (&wi, 0, sizeof (wi)); - wi.gsi = gsi; - walk_gimple_op (stmt, instrument_null, &wi); + if (flag_sanitize & SANITIZE_NULL) + { + if (gimple_store_p (stmt)) + instrument_null (gsi, true); + if (gimple_assign_load_p (stmt)) + instrument_null (gsi, false); + } + gsi_next (&gsi); } } --- gcc/testsuite/g++.dg/ubsan/pr59306.C.mp3 2013-11-27 11:15:17.900456072 +0100 +++ gcc/testsuite/g++.dg/ubsan/pr59306.C 2013-11-27 11:15:01.696387988 +0100 @@ -0,0 +1,14 @@ +// { dg-do compile } +// { dg-options "-fsanitize=undefined" } +// { dg-skip-if "" { *-*-* } { "-flto" } { "" } } + +class A { + void bar (void (A::*) (int)); + void foo (int); + void B (); +}; + +void A::B() +{ + bar (&A::foo); +} Marek