+2010-12-07 Jakub Jelinek <jakub@redhat.com>
+
+ PR middle-end/46534
+ * builtins.c (expand_builtin_printf): Don't copy and modify string
+ before build_string_literal, instead modify what
+ build_string_literal returned.
+
+ Backport from mainline
+ 2010-11-18 Jakub Jelinek <jakub@redhat.com>
+
+ PR middle-end/46534
+ * builtins.c (fold_builtin_printf): Don't copy and modify string
+ before build_string_literal, instead modify what
+ build_string_literal returned.
+
2010-12-05 Richard Guenther <rguenther@suse.de>
Ira Rosen <irar@il.ibm.com>
{
/* If the format specifier was "string\n", call puts("string"). */
size_t len = strlen (fmt_str);
- if ((unsigned char)fmt_str[len - 1] == target_newline)
+ if ((unsigned char)fmt_str[len - 1] == target_newline
+ && (size_t) (int) len == len
+ && (int) len > 0)
{
+ char *newstr;
+ tree offset_node, string_cst;
+
/* Create a NUL-terminated string that's one char shorter
than the original, stripping off the trailing '\n'. */
- char *newstr = XALLOCAVEC (char, len);
- memcpy (newstr, fmt_str, len - 1);
- newstr[len - 1] = 0;
- arg = build_string_literal (len, newstr);
+ arg = build_string_literal (len, fmt_str);
+ string_cst = string_constant (arg, &offset_node);
+#ifdef ENABLE_CHECKING
+ gcc_assert (string_cst
+ && (TREE_STRING_LENGTH (string_cst)
+ == (int) len)
+ && integer_zerop (offset_node)
+ && (unsigned char)
+ TREE_STRING_POINTER (string_cst)[len - 1]
+ == target_newline);
+#endif
+ /* build_string_literal creates a new STRING_CST,
+ modify it in place to avoid double copying. */
+ newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
+ newstr[len - 1] = '\0';
if (fn_puts)
fn = build_call_expr (fn_puts, 1, arg);
}
{
/* If the string was "string\n", call puts("string"). */
size_t len = strlen (str);
- if ((unsigned char)str[len - 1] == target_newline)
+ if ((unsigned char)str[len - 1] == target_newline
+ && (size_t) (int) len == len
+ && (int) len > 0)
{
+ char *newstr;
+ tree offset_node, string_cst;
+
/* Create a NUL-terminated string that's one char shorter
than the original, stripping off the trailing '\n'. */
- char *newstr = XALLOCAVEC (char, len);
- memcpy (newstr, str, len - 1);
- newstr[len - 1] = 0;
-
- newarg = build_string_literal (len, newstr);
+ newarg = build_string_literal (len, str);
+ string_cst = string_constant (newarg, &offset_node);
+#ifdef ENABLE_CHECKING
+ gcc_assert (string_cst
+ && (TREE_STRING_LENGTH (string_cst)
+ == (int) len)
+ && integer_zerop (offset_node)
+ && (unsigned char)
+ TREE_STRING_POINTER (string_cst)[len - 1]
+ == target_newline);
+#endif
+ /* build_string_literal creates a new STRING_CST,
+ modify it in place to avoid double copying. */
+ newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
+ newstr[len - 1] = '\0';
if (fn_puts)
call = build_call_expr (fn_puts, 1, newarg);
}