Hi.
The function c_getstr returns string and length of the string.
In inline_expand_builtin_string_cmp, we should consider situations
where a string constant contains a zero character. In that case
we have to shorten len[12].
Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
Ready to be installed?
Thanks,
Martin
gcc/ChangeLog:
2019-06-17 Martin Liska <[email protected]>
PR tree-optimization/90892
* builtins.c (inline_expand_builtin_string_cmp): Handle '\0'
in string constants.
gcc/testsuite/ChangeLog:
2019-06-17 Martin Liska <[email protected]>
PR tree-optimization/90892
* gcc.dg/pr90892.c: New test.
---
gcc/builtins.c | 14 ++++++++++++++
gcc/testsuite/gcc.dg/pr90892.c | 14 ++++++++++++++
2 files changed, 28 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/pr90892.c
diff --git a/gcc/builtins.c b/gcc/builtins.c
index 3463ffb1539..917852071b9 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -7142,6 +7142,20 @@ inline_expand_builtin_string_cmp (tree exp, rtx target)
const char *src_str1 = c_getstr (arg1, &len1);
const char *src_str2 = c_getstr (arg2, &len2);
+ if (src_str1 != NULL)
+ {
+ unsigned HOST_WIDE_INT str_str1_strlen = strlen (src_str1);
+ if (str_str1_strlen + 1 < len1)
+ len1 = str_str1_strlen + 1;
+ }
+
+ if (src_str2 != NULL)
+ {
+ unsigned HOST_WIDE_INT str_str2_strlen = strlen (src_str2);
+ if (str_str2_strlen + 1 < len2)
+ len2 = str_str2_strlen + 1;
+ }
+
/* If neither strings is constant string, the call is not qualify. */
if (!src_str1 && !src_str2)
return NULL_RTX;
diff --git a/gcc/testsuite/gcc.dg/pr90892.c b/gcc/testsuite/gcc.dg/pr90892.c
new file mode 100644
index 00000000000..e4b5310807a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr90892.c
@@ -0,0 +1,14 @@
+/* PR tree-optimization/90892 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+const char *a = "A\0b";
+
+int
+main()
+{
+ if (__builtin_strncmp(a, "A\0", 2) != 0)
+ __builtin_abort ();
+
+ return 0;
+}