]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libcpp: Fix ICE on directive inside _Pragma() operator [PR67046]
authorLewis Hyatt <lhyatt@gmail.com>
Sat, 14 Jan 2023 18:29:22 +0000 (13:29 -0500)
committerLewis Hyatt <lhyatt@gmail.com>
Mon, 13 Mar 2023 11:40:50 +0000 (07:40 -0400)
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.

gcc/testsuite/c-c++-common/cpp/pr67046.c [new file with mode: 0644]
libcpp/directives.cc

diff --git a/gcc/testsuite/c-c++-common/cpp/pr67046.c b/gcc/testsuite/c-c++-common/cpp/pr67046.c
new file mode 100644 (file)
index 0000000..f37f20c
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do preprocess } */
+
+_Pragma(
+"message(\"msg\")"
+)
+
+_Pragma(
+"message(\"msg\")"
+#
+)
index 7f84b83ecdcd647ac0ece6af10b7711e43aa8308..1fdd73edcb8696044bb78b059e140f7408d6628e 100644 (file)
@@ -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)