From f451d3a836fff435bc4bc0ed121cdc5f6e6c14d6 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Sat, 22 Mar 2014 17:25:50 +0100 Subject: re PR sanitizer/60613 (Invalid signed subtraction ubsan diagnostics) PR sanitizer/60613 * internal-fn.c (ubsan_expand_si_overflow_addsub_check): For code == MINUS_EXPR, never swap op0 with op1. * c-c++-common/ubsan/pr60613-1.c: New test. * c-c++-common/ubsan/pr60613-2.c: New test. From-SVN: r208766 --- gcc/internal-fn.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'gcc/internal-fn.c') diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c index 9926ec2..1062ea8 100644 --- a/gcc/internal-fn.c +++ b/gcc/internal-fn.c @@ -221,14 +221,15 @@ ubsan_expand_si_overflow_addsub_check (tree_code code, gimple stmt) res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab, op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN); - /* If we can prove one of the arguments is always non-negative - or always negative, we can do just one comparison and - conditional jump instead of 2 at runtime, 3 present in the + /* If we can prove one of the arguments (for MINUS_EXPR only + the second operand, as subtraction is not commutative) is always + non-negative or always negative, we can do just one comparison + and conditional jump instead of 2 at runtime, 3 present in the emitted code. If one of the arguments is CONST_INT, all we need is to make sure it is op1, then the first emit_cmp_and_jump_insns will be just folded. Otherwise try to use range info if available. */ - if (CONST_INT_P (op0)) + if (code == PLUS_EXPR && CONST_INT_P (op0)) { rtx tem = op0; op0 = op1; @@ -236,7 +237,7 @@ ubsan_expand_si_overflow_addsub_check (tree_code code, gimple stmt) } else if (CONST_INT_P (op1)) ; - else if (TREE_CODE (arg0) == SSA_NAME) + else if (code == PLUS_EXPR && TREE_CODE (arg0) == SSA_NAME) { double_int arg0_min, arg0_max; if (get_range_info (arg0, &arg0_min, &arg0_max) == VR_RANGE) -- cgit v1.1