public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: ibuclaw@gdcproject.org
To: gcc-patches@gcc.gnu.org
Subject: Re: [committed] d: Fix ICE in when formating a string with '%' or '`' characters (PR98457)
Date: Mon, 19 Apr 2021 22:47:06 +0200 (CEST)	[thread overview]
Message-ID: <1012050353.105441.1618865226702@office.mailbox.org> (raw)
In-Reply-To: <20210419175057.3761899-1-ibuclaw@gdcproject.org>

> On 19/04/2021 19:50 Iain Buclaw <ibuclaw@gdcproject.org> wrote:
> 
>  
> Hi,
> 
> This patch fixes an ICE that occurred in the D front-end diagnostic
> handlers.  The percentage character was being confused for a format
> specifier in pp_format(), whilst the backtick character was confused for
> the beginning of a quoted string in expand_d_format().
> 
> Both are now properly escaped to avoid the ICE.
> 
> Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, and
> committed to mainline.
> 
> Will also be preparing a backport to the gcc-10 and gcc-9 release
> branches, as the bug is reproducible there as well.

Attached backport, regression tested and applied to both releases/gcc-9
and releases/gcc-10 branches.

Regards,
Iain.

---
gcc/d/ChangeLog:

	PR d/98457
	* d-diagnostic.cc (expand_d_format): Handle escaped backticks.
	(escape_d_format): New funtion.
	(verror): Call escape_d_format on prefixing strings.
	(vdeprecation): Likewise.

gcc/testsuite/ChangeLog:

	PR d/98457
	* gdc.dg/pr98457.d: New test.

(cherry picked from commit dc7d1c74ffb1cc85e67984632f581d526c783770)
---
 gcc/d/d-diagnostic.cc          | 64 +++++++++++++++++++++++++++++++---
 gcc/testsuite/gdc.dg/pr98457.d |  9 +++++
 2 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gdc.dg/pr98457.d

diff --git a/gcc/d/d-diagnostic.cc b/gcc/d/d-diagnostic.cc
index 95a008d1b6f..47c7323aa0b 100644
--- a/gcc/d/d-diagnostic.cc
+++ b/gcc/d/d-diagnostic.cc
@@ -48,7 +48,7 @@ expand_d_format (const char *format)
 
   for (const char *p = format; *p;)
     {
-      while (*p != '\0' && *p != '%' && *p != '`')
+      while (*p != '\0' && *p != '\\' && *p != '%' && *p != '`')
 	{
 	  buf.writeByte (*p);
 	  p++;
@@ -57,6 +57,21 @@ expand_d_format (const char *format)
       if (*p == '\0')
 	break;
 
+      if (*p == '\\')
+	{
+	  if (p[1] == '`')
+	    {
+	      /* Escaped backtick, don't expand it as a quoted string.  */
+	      buf.writeByte ('`');
+	      p++;;
+	    }
+	  else
+	    buf.writeByte (*p);
+
+	  p++;
+	  continue;
+	}
+
       if (*p == '`')
 	{
 	  /* Text enclosed by `...` are translated as a quoted string.  */
@@ -113,6 +128,43 @@ expand_d_format (const char *format)
   return buf.extractString ();
 }
 
+/* Rewrite the format string FORMAT to deal with any characters that require
+   escaping before expand_d_format expands it.  */
+
+static char *
+escape_d_format (const char *format)
+{
+  obstack buf;
+
+  gcc_obstack_init (&buf);
+
+  for (const char *p = format; *p; p++)
+    {
+      switch (*p)
+	{
+	case '%':
+	  /* Escape `%' characters so that pp_format does not confuse them
+	     for actual format specifiers.  */
+	  obstack_1grow (&buf, '%');
+	  break;
+
+	case '`':
+	  /* Escape '`' characters so that expand_d_format does not confuse them
+	     for a quoted string.  */
+	  obstack_1grow (&buf, '\\');
+	  break;
+
+	default:
+	  break;
+	}
+
+      obstack_1grow (&buf, *p);
+    }
+
+  obstack_1grow (&buf, '\0');
+  return (char *) obstack_finish (&buf);
+}
+
 /* Helper routine for all error routines.  Reports a diagnostic specified by
    KIND at the explicit location LOC.  The message FORMAT comes from the dmd
    front-end, which does not get translated by the gcc diagnostic routines.  */
@@ -177,9 +229,10 @@ verror (const Loc& loc, const char *format, va_list ap,
 
       /* Build string and emit.  */
       if (prefix2 != NULL)
-	xformat = xasprintf ("%s %s %s", prefix1, prefix2, format);
+	xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
+			     escape_d_format (prefix2), format);
       else if (prefix1 != NULL)
-	xformat = xasprintf ("%s %s", prefix1, format);
+	xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
       else
 	xformat = xasprintf ("%s", format);
 
@@ -287,9 +340,10 @@ vdeprecation (const Loc& loc, const char *format, va_list ap,
 
       /* Build string and emit.  */
       if (prefix2 != NULL)
-	xformat = xasprintf ("%s %s %s", prefix1, prefix2, format);
+	xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
+			     escape_d_format (prefix2), format);
       else if (prefix1 != NULL)
-	xformat = xasprintf ("%s %s", prefix1, format);
+	xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
       else
 	xformat = xasprintf ("%s", format);
 
diff --git a/gcc/testsuite/gdc.dg/pr98457.d b/gcc/testsuite/gdc.dg/pr98457.d
new file mode 100644
index 00000000000..bc0d8af5d4a
--- /dev/null
+++ b/gcc/testsuite/gdc.dg/pr98457.d
@@ -0,0 +1,9 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98457
+// { dg-do compile }
+
+void main()
+{
+    writef!"%s";    // { dg-error "template instance writef!\"%s\" template .writef. is not defined" }
+    writef!"`%s";   // { dg-error "template instance writef!\"`%s\" template .writef. is not defined" }
+    writef!"%%s`";  // { dg-error "template instance writef!\"%%s`\" template .writef. is not defined" }
+}
-- 
2.27.0

      reply	other threads:[~2021-04-19 20:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19 17:50 Iain Buclaw
2021-04-19 20:47 ` ibuclaw [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1012050353.105441.1618865226702@office.mailbox.org \
    --to=ibuclaw@gdcproject.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).