summaryrefslogtreecommitdiff
path: root/gcc/c-family/c-ubsan.c
diff options
context:
space:
mode:
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2013-09-25 08:58:57 +0000
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2013-09-25 08:58:57 +0000
commit51f553af8bd7e56c2e987dde01a96abbb93c2e7c (patch)
tree083cf293ac56951841b59e2db56b5176d154c73a /gcc/c-family/c-ubsan.c
parentfd19a5dc0c089c236961b0f313377fe4d5ea70a8 (diff)
downloadgcc-51f553af8bd7e56c2e987dde01a96abbb93c2e7c.tar.gz
PR sanitizer/58413
c-family/ * c-ubsan.c (ubsan_instrument_shift): Don't instrument an expression if we can prove it is correct. (ubsan_instrument_division): Likewise. Remove unnecessary check. testsuite/ * c-c++-common/ubsan/shift-5.c: New test. * c-c++-common/ubsan/shift-6.c: New test. * c-c++-common/ubsan/div-by-zero-5.c: New test. * gcc.dg/ubsan/c-shift-1.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@202886 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/c-family/c-ubsan.c')
-rw-r--r--gcc/c-family/c-ubsan.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/gcc/c-family/c-ubsan.c b/gcc/c-family/c-ubsan.c
index 9f43f6d55b8..0bfc660cdd5 100644
--- a/gcc/c-family/c-ubsan.c
+++ b/gcc/c-family/c-ubsan.c
@@ -51,14 +51,6 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
if (TREE_CODE (type) != INTEGER_TYPE)
return NULL_TREE;
- /* If we *know* that the divisor is not -1 or 0, we don't have to
- instrument this expression.
- ??? We could use decl_constant_value to cover up more cases. */
- if (TREE_CODE (op1) == INTEGER_CST
- && integer_nonzerop (op1)
- && !integer_minus_onep (op1))
- return NULL_TREE;
-
t = fold_build2 (EQ_EXPR, boolean_type_node,
op1, build_int_cst (type, 0));
@@ -74,6 +66,11 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, x);
}
+ /* If the condition was folded to 0, no need to instrument
+ this expression. */
+ if (integer_zerop (t))
+ return NULL_TREE;
+
/* In case we have a SAVE_EXPR in a conditional context, we need to
make sure it gets evaluated before the condition. */
t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), op0, t);
@@ -138,6 +135,11 @@ ubsan_instrument_shift (location_t loc, enum tree_code code,
tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
}
+ /* If the condition was folded to 0, no need to instrument
+ this expression. */
+ if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
+ return NULL_TREE;
+
/* In case we have a SAVE_EXPR in a conditional context, we need to
make sure it gets evaluated before the condition. */
t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), op0, t);