public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-6631] libcpp: Fix ICE on directive inside _Pragma() operator [PR67046]
@ 2023-03-13 13:08 Lewis Hyatt
  0 siblings, 0 replies; only message in thread
From: Lewis Hyatt @ 2023-03-13 13:08 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:336ce497fb266268a79544f00dd3bbd24cf3a4d6

commit r13-6631-g336ce497fb266268a79544f00dd3bbd24cf3a4d6
Author: Lewis Hyatt <lhyatt@gmail.com>
Date:   Sat Jan 14 13:29:22 2023 -0500

    libcpp: Fix ICE on directive inside _Pragma() operator [PR67046]
    
    get__Pragma_string() in directives.cc is responsible for lexing the parens
    and the string argument from a _Pragma("...") operator. This function does
    not handle the case when the closing paren is not on the same line as the
    string; in that case, libcpp will by default reuse the token buffer it
    previously used for the string, so that the string token returned by
    get__Pragma_string() may be corrupted, as shown in the testcase. Fix using
    the existing keep_tokens mechanism that temporarily disables the reuse of
    token buffers.
    
    libcpp/ChangeLog:
    
            PR preprocessor/67046
            * directives.cc (_cpp_do__Pragma): Increment pfile->keep_tokens to
            ensure the returned string token is valid.
    
    gcc/testsuite/ChangeLog:
    
            PR preprocessor/67046
            * c-c++-common/cpp/pr67046.c: New test.

Diff:
---
 gcc/testsuite/c-c++-common/cpp/pr67046.c | 10 ++++++++++
 libcpp/directives.cc                     |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/gcc/testsuite/c-c++-common/cpp/pr67046.c b/gcc/testsuite/c-c++-common/cpp/pr67046.c
new file mode 100644
index 00000000000..f37f20c624e
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/pr67046.c
@@ -0,0 +1,10 @@
+/* { dg-do preprocess } */
+
+_Pragma(
+"message(\"msg\")"
+)
+
+_Pragma(
+"message(\"msg\")"
+#
+)
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 7f84b83ecdc..1fdd73edcb8 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1996,7 +1996,12 @@ destringize_and_run (cpp_reader *pfile, const cpp_string *in,
 int
 _cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc)
 {
+  /* Make sure we don't invalidate the string token, if the closing parenthesis
+   ended up on a different line.  */
+  ++pfile->keep_tokens;
   const cpp_token *string = get__Pragma_string (pfile);
+  --pfile->keep_tokens;
+
   pfile->directive_result.type = CPP_PADDING;
 
   if (string)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-13 13:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-13 13:08 [gcc r13-6631] libcpp: Fix ICE on directive inside _Pragma() operator [PR67046] Lewis Hyatt

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).