aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-06-29 11:24:38 +0200
committerJakub Jelinek <jakub@redhat.com>2021-06-29 11:24:38 +0200
commit53fd7544aff6d0a18869017cb9bb921a7f5dcd04 (patch)
tree710f2740fdeb90cb0f0fb62a1e619a90b67b3f18 /gcc/match.pd
parentc01760bc548ba79bc9ac15168b27fe7aabcb19ae (diff)
downloadgcc-53fd7544aff6d0a18869017cb9bb921a7f5dcd04.zip
gcc-53fd7544aff6d0a18869017cb9bb921a7f5dcd04.tar.gz
gcc-53fd7544aff6d0a18869017cb9bb921a7f5dcd04.tar.bz2
match.pd: Avoid (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST opt in GENERIC when sanitizing [PR101210]
When we have (intptr_t) x == cst where x has REFERENCE_TYPE, this optimization creates x == cst out of it where cst has REFERENCE_TYPE. If it is done in GENERIC folding, it can results in ubsan failures where the INTEGER_CST with REFERENCE_TYPE is instrumented. Fixed by deferring it to GIMPLE folding in this case. 2021-06-29 Jakub Jelinek <jakub@redhat.com> PR c++/101210 * match.pd ((intptr_t)x eq/ne CST to x eq/ne (typeof x) CST): Don't perform the optimization in GENERIC when sanitizing and x has a reference type. * g++.dg/ubsan/pr101210.C: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd7
1 files changed, 6 insertions, 1 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 39fb57e..8205271 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5124,7 +5124,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cmp (convert @0) INTEGER_CST@1)
(if (((POINTER_TYPE_P (TREE_TYPE (@0))
&& !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
- && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
+ && INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ /* Don't perform this optimization in GENERIC if @0 has reference
+ type when sanitizing. See PR101210. */
+ && !(GENERIC
+ && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
+ && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
|| (INTEGRAL_TYPE_P (TREE_TYPE (@0))
&& POINTER_TYPE_P (TREE_TYPE (@1))
&& !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))