Hi!

cpp_peek_token is broken whenever it sees CPP_EOF, as it doesn't back up
the previous tokens (if any) and the CPP_EOF token, so for callers
of cpp_peek_token whenever it returns CPP_EOF it is a fatal condition,
so one really can't peek safely.

cpp_peek_token has apparently been written for the conditional macros
where it really assumes one can safely peek tokens several times.

The following patch fixes that.  If no CPP_EOF is seen or it is seen on the
last token, index will be -1 and thus count - index is still count + 1
as before, but if we hit CPP_EOF earlier, we only back up tokens before
that.

Richard and Prathamesh raised this issue already some time ago:
https://gcc.gnu.org/ml/gcc-patches/2014-08/msg00895.html
but there was no follow-up on that.

Bootstrapped/regtested on powerpc64{,le}-linux and {x86_64,i686}-linux, ok
for trunk?

2015-03-31  Jakub Jelinek  <ja...@redhat.com>

        PR preprocessor/61977
        * lex.c (cpp_peek_token): If peektok is CPP_EOF, back it up
        with all tokens peeked by the current function.

--- libcpp/lex.c.jj     2015-03-17 18:10:12.000000000 +0100
+++ libcpp/lex.c        2015-03-31 19:39:28.207748285 +0200
@@ -2084,11 +2084,14 @@ cpp_peek_token (cpp_reader *pfile, int i
     {
       peektok = _cpp_lex_token (pfile);
       if (peektok->type == CPP_EOF)
-       return peektok;
+       {
+         index--;
+         break;
+       }
     }
   while (index--);
 
-  _cpp_backup_tokens_direct (pfile, count + 1);
+  _cpp_backup_tokens_direct (pfile, count - index);
   pfile->keep_tokens--;
 
   return peektok;
--- gcc/testsuite/gcc.dg/cpp/pr61977.c.jj       2015-03-31 19:46:12.226279905 
+0200
+++ gcc/testsuite/gcc.dg/cpp/pr61977.c  2015-03-31 19:46:03.638417398 +0200
@@ -0,0 +1,4 @@
+/* PR preprocessor/61977 */
+/* { dg-do preprocess } */
+
+vector

        Jakub

Reply via email to