public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
@ 2016-05-17 20:47 Paolo Carlini
  2016-05-18 14:39 ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Carlini @ 2016-05-17 20:47 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 476 bytes --]

Hi,

this ICE during error recovery exposes a rather more general weakness: 
we should never call cp_lexer_peek_nth_token (*, 2) when a previous 
cp_lexer_peek_token returns CPP_EOF.

The fix seems easy, just reshape a bit the condition and delay the 
latter. It should also be a net, albeit minor, performance win: in 
general we don't want to call cp_lexer_peek_nth_token (which is out of 
line) unnecessarily.

Tested x86_64-linux.

Thanks,
Paolo.

///////////////////////

[-- Attachment #2: CL_69793 --]
[-- Type: text/plain, Size: 318 bytes --]

/cp
2016-05-17  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/69793
	* parser.c (cp_parser_template_id): Don't call cp_lexer_peek_nth_token
	when the previous cp_lexer_peek_token returns CPP_EOF.

/testsuite
2016-05-17  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/69793
	* g++.dg/template/crash123.C: New.

[-- Attachment #3: patch_69793 --]
[-- Type: text/plain, Size: 1303 bytes --]

Index: cp/parser.c
===================================================================
--- cp/parser.c	(revision 236338)
+++ cp/parser.c	(working copy)
@@ -14835,11 +14835,11 @@ cp_parser_template_id (cp_parser *parser,
   /* If we find the sequence `[:' after a template-name, it's probably
      a digraph-typo for `< ::'. Substitute the tokens and check if we can
      parse correctly the argument list.  */
-  next_token = cp_lexer_peek_token (parser->lexer);
-  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
-  if (next_token->type == CPP_OPEN_SQUARE
+  if (((next_token = cp_lexer_peek_token (parser->lexer))->type
+       == CPP_OPEN_SQUARE)
       && next_token->flags & DIGRAPH
-      && next_token_2->type == CPP_COLON
+      && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
+	  == CPP_COLON)
       && !(next_token_2->flags & PREV_WHITE))
     {
       cp_parser_parse_tentatively (parser);
Index: testsuite/g++.dg/template/crash123.C
===================================================================
--- testsuite/g++.dg/template/crash123.C	(revision 0)
+++ testsuite/g++.dg/template/crash123.C	(working copy)
@@ -0,0 +1,4 @@
+// PR c++/69793
+
+class fpos;
+template < state > bool operator!= (fpos,; operator!= // { dg-error "declared|expected|type" }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
  2016-05-17 20:47 [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"") Paolo Carlini
@ 2016-05-18 14:39 ` Jason Merrill
  2016-05-18 15:05   ` Paolo Carlini
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2016-05-18 14:39 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

On 05/17/2016 04:47 PM, Paolo Carlini wrote:
> this ICE during error recovery exposes a rather more general weakness:
> we should never call cp_lexer_peek_nth_token (*, 2) when a previous
> cp_lexer_peek_token returns CPP_EOF.

Hmm, that seems fragile, I would expect it to keep returning EOF.

But your patch is OK.

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
  2016-05-18 14:39 ` Jason Merrill
@ 2016-05-18 15:05   ` Paolo Carlini
  2016-05-18 15:18     ` Jason Merrill
  2016-05-18 15:29     ` David Malcolm
  0 siblings, 2 replies; 6+ messages in thread
From: Paolo Carlini @ 2016-05-18 15:05 UTC (permalink / raw)
  To: Jason Merrill, gcc-patches

Hi,

On 18/05/2016 16:39, Jason Merrill wrote:
> On 05/17/2016 04:47 PM, Paolo Carlini wrote:
>> this ICE during error recovery exposes a rather more general weakness:
>> we should never call cp_lexer_peek_nth_token (*, 2) when a previous
>> cp_lexer_peek_token returns CPP_EOF.
>
> Hmm, that seems fragile, I would expect it to keep returning EOF.
Indeed. I didn't explain myself well enough. I meant something along the 
lines: outside this specific and minor case of ICE during error 
recovery, we should audit our code and keep in mind that calling 
cp_lexer_peek_nth_token (*, anything > 1, the common case) right after 
cp_lexer_peek_token is, how shall I put it, "suspect", due to that 
assert at the beginning of cp_lexer_peek_nth_token.
> But your patch is OK.
Thanks. I'm going to commit it then.

Paolo.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
  2016-05-18 15:05   ` Paolo Carlini
@ 2016-05-18 15:18     ` Jason Merrill
  2016-05-18 15:39       ` Paolo Carlini
  2016-05-18 15:29     ` David Malcolm
  1 sibling, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2016-05-18 15:18 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

On 05/18/2016 11:05 AM, Paolo Carlini wrote:
> On 18/05/2016 16:39, Jason Merrill wrote:
>> On 05/17/2016 04:47 PM, Paolo Carlini wrote:
>>> this ICE during error recovery exposes a rather more general weakness:
>>> we should never call cp_lexer_peek_nth_token (*, 2) when a previous
>>> cp_lexer_peek_token returns CPP_EOF.

>> Hmm, that seems fragile, I would expect it to keep returning EOF.

> Indeed. I didn't explain myself well enough. I meant something along the
> lines: outside this specific and minor case of ICE during error
> recovery, we should audit our code and keep in mind that calling
> cp_lexer_peek_nth_token (*, anything > 1, the common case) right after
> cp_lexer_peek_token is, how shall I put it, "suspect", due to that
> assert at the beginning of cp_lexer_peek_nth_token.

I understood that, but I think that assert should be replaced with code 
to properly handle that case.

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
  2016-05-18 15:05   ` Paolo Carlini
  2016-05-18 15:18     ` Jason Merrill
@ 2016-05-18 15:29     ` David Malcolm
  1 sibling, 0 replies; 6+ messages in thread
From: David Malcolm @ 2016-05-18 15:29 UTC (permalink / raw)
  To: Paolo Carlini, Jason Merrill, gcc-patches

On Wed, 2016-05-18 at 17:05 +0200, Paolo Carlini wrote:
> Hi,
> 
> On 18/05/2016 16:39, Jason Merrill wrote:
> > On 05/17/2016 04:47 PM, Paolo Carlini wrote:
> > > this ICE during error recovery exposes a rather more general
> > > weakness:
> > > we should never call cp_lexer_peek_nth_token (*, 2) when a
> > > previous
> > > cp_lexer_peek_token returns CPP_EOF.
> > 
> > Hmm, that seems fragile, I would expect it to keep returning EOF.
> Indeed. I didn't explain myself well enough. I meant something along
> the 
> lines: outside this specific and minor case of ICE during error 
> recovery, we should audit our code and keep in mind that calling 
> cp_lexer_peek_nth_token (*, anything > 1, the common case) right
> after 
> cp_lexer_peek_token is, how shall I put it, "suspect", due to that 
> assert at the beginning of cp_lexer_peek_nth_token.

Thinking aloud, I wonder if a plugin could detect this kind of thing? -
verify that any call of cp_lexer_peek_nth_token for N > 1 happens after
the result of cp_lexer_peek_token has been checked for EOF.

(another idea might be a fault-injection option for the C++ frontend,
perhaps simulating EOF at a given token index, and then using this,
somehow, to torture-test the existing test cases, but that seems like a
*lot* of extra parsing; static checking seems more efficient).

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"")
  2016-05-18 15:18     ` Jason Merrill
@ 2016-05-18 15:39       ` Paolo Carlini
  0 siblings, 0 replies; 6+ messages in thread
From: Paolo Carlini @ 2016-05-18 15:39 UTC (permalink / raw)
  To: Jason Merrill, gcc-patches

Hi,

On 18/05/2016 17:17, Jason Merrill wrote:
> On 05/18/2016 11:05 AM, Paolo Carlini wrote:
>> On 18/05/2016 16:39, Jason Merrill wrote:
>>> On 05/17/2016 04:47 PM, Paolo Carlini wrote:
>>>> this ICE during error recovery exposes a rather more general weakness:
>>>> we should never call cp_lexer_peek_nth_token (*, 2) when a previous
>>>> cp_lexer_peek_token returns CPP_EOF.
>
>>> Hmm, that seems fragile, I would expect it to keep returning EOF.
>
>> Indeed. I didn't explain myself well enough. I meant something along the
>> lines: outside this specific and minor case of ICE during error
>> recovery, we should audit our code and keep in mind that calling
>> cp_lexer_peek_nth_token (*, anything > 1, the common case) right after
>> cp_lexer_peek_token is, how shall I put it, "suspect", due to that
>> assert at the beginning of cp_lexer_peek_nth_token.
>
> I understood that, but I think that assert should be replaced with 
> code to properly handle that case.
Ah yes, that comment before cp_lexer_peek_nth_token. I read it 
yesterday, got interested, but afterward focused on the specific issue 
in the bug report, seemed easy to fix.

Paolo.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-05-18 15:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-17 20:47 [C++ Patch] PR 69793 ("ICE on invalid code in "cp_lexer_peek_nth_token"") Paolo Carlini
2016-05-18 14:39 ` Jason Merrill
2016-05-18 15:05   ` Paolo Carlini
2016-05-18 15:18     ` Jason Merrill
2016-05-18 15:39       ` Paolo Carlini
2016-05-18 15:29     ` David Malcolm

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