This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix strpbrk (x, "") folding (PR c/84953)


Hi!

We use incorrect type for the NULL return value, the builtin is
char *strpbrk (const char *, const char *), so the first argument
is cast to const char * and we return (const char *) 0, while we
really should return (char *) 0.  fold_builtin_n then adds:
      ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
      SET_EXPR_LOCATION (ret, loc);
      TREE_NO_WARNING (ret) = 1;
and thus we in the end have (char *) (_Literal (const char *) 0).
This bug then results in a -Wdiscarded-qualifiers warning.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2018-03-20  Jakub Jelinek  <jakub@redhat.com>

	PR c/84953
	* builtins.c (fold_builtin_strpbrk): For strpbrk(x, "") use type
	instead of TREE_TYPE (s1) for the return value.

	* gcc.dg/pr84953.c: New test.

--- gcc/builtins.c.jj	2018-03-07 22:51:58.871478732 +0100
+++ gcc/builtins.c	2018-03-19 18:49:45.313898848 +0100
@@ -9573,7 +9573,7 @@ fold_builtin_strpbrk (location_t loc, tr
       if (p2[0] == '\0')
 	/* strpbrk(x, "") == NULL.
 	   Evaluate and ignore s1 in case it had side-effects.  */
-	return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
+	return omit_one_operand_loc (loc, type, integer_zero_node, s1);
 
       if (p2[1] != '\0')
 	return NULL_TREE;  /* Really call strpbrk.  */
--- gcc/testsuite/gcc.dg/pr84953.c.jj	2018-03-19 18:52:48.295893571 +0100
+++ gcc/testsuite/gcc.dg/pr84953.c	2018-03-19 18:52:31.935894043 +0100
@@ -0,0 +1,11 @@
+/* PR c/84953 */
+/* { dg-do compile } */
+
+char *strpbrk (const char *, const char *);
+
+char *
+test (char *p)
+{
+  p = strpbrk (p, "");	/* { dg-bogus "assignment discards 'const' qualifier from pointer target type" } */
+  return p;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]