diff options
author | Jun Ma <JunMa@linux.alibaba.com> | 2019-06-27 09:50:35 +0000 |
---|---|---|
committer | Jun Ma <junma@gcc.gnu.org> | 2019-06-27 09:50:35 +0000 |
commit | 5fd336bbd7b273408018dfbb7a170e1d54facb8e (patch) | |
tree | 22f6406d9cbec125a110e719d1d92089c273176e /gcc/gimple-fold.c | |
parent | e11c48711128a8aff3938cf28954acf1b1cc0b62 (diff) | |
download | gcc-5fd336bbd7b273408018dfbb7a170e1d54facb8e.zip gcc-5fd336bbd7b273408018dfbb7a170e1d54facb8e.tar.gz gcc-5fd336bbd7b273408018dfbb7a170e1d54facb8e.tar.bz2 |
re PR tree-optimization/89772 (memchr for a character not in constant nul-padded string not folded)
PR tree-optimization/89772
* gimple-fold.c (gimple_fold_builtin_memchr): consider trailing nuls in
out-of-bound accesses checking.
gcc/testsuite
* gcc.dg/builtin-memchr-4.c: New test.
From-SVN: r272740
Diffstat (limited to 'gcc/gimple-fold.c')
-rw-r--r-- | gcc/gimple-fold.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index dfb31a0..118718a 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -2543,7 +2543,15 @@ gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) const char *r = (const char *)memchr (p1, c, MIN (length, string_length)); if (r == NULL) { - if (length <= string_length) + tree mem_size, offset_node; + string_constant (arg1, &offset_node, &mem_size, NULL); + unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE) + ? 0 : tree_to_uhwi (offset_node); + /* MEM_SIZE is the size of the array the string literal + is stored in. */ + unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset; + gcc_checking_assert (string_length <= string_size); + if (length <= string_size) { replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0)); return true; |