aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-05-23 23:48:20 -0400
committerJason Merrill <jason@redhat.com>2022-05-25 11:08:57 -0400
commit8c9c92f8079589730708ce831a86e01d510d9db4 (patch)
tree8e35424fe8a5f785937273e76056bdcb81094887
parent6209009df65ff68482ef66951856f50cf362d990 (diff)
downloadgcc-8c9c92f8079589730708ce831a86e01d510d9db4.zip
gcc-8c9c92f8079589730708ce831a86e01d510d9db4.tar.gz
gcc-8c9c92f8079589730708ce831a86e01d510d9db4.tar.bz2
c++: constexpr returning deallocated ptr
In constexpr-new3.C, the f7 function returns a deleted pointer, which we were happily caching because the new and delete are balanced. Don't. gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_call_expression): Check for heap vars in the result.
-rw-r--r--gcc/cp/constexpr.cc5
1 files changed, 5 insertions, 0 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 1a70fda..4520847 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -1356,6 +1356,7 @@ static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
value_cat, bool *, bool *, tree * = NULL);
static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
bool * = NULL);
+static tree find_heap_var_refs (tree *, int *, void *);
/* Attempt to evaluate T which represents a call to a builtin function.
We assume here that all builtin functions evaluate to scalar types
@@ -2965,6 +2966,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
cacheable = false;
break;
}
+ /* Also don't cache a call that returns a deallocated pointer. */
+ if (cacheable && (cp_walk_tree_without_duplicates
+ (&result, find_heap_var_refs, NULL)))
+ cacheable = false;
}
/* Rewrite all occurrences of the function's RESULT_DECL with the