diff mbox

[GSoC,match-and-simplify] put check for EOF

Message ID CAJXstsBHDKa-6xENNrTAD75M1bMU5iPzrw9fAAKg0cPmCoxcEQ@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni Aug. 8, 2014, 9:50 a.m. UTC
Put check for EOF in parse_for and parse_if,
else it results in segmentation fault.

eg:
(if (0)
  <eof>

Not sure why it segfaults.
I got following back-trace (in gdb):

#0  _cpp_lex_direct (pfile=pfile@entry=0x66f560) at ../../src/libcpp/lex.c:2183
#1  0x000000000041d50c in _cpp_lex_token (pfile=pfile@entry=0x66f560)
at ../../src/libcpp/lex.c:2067
#2  0x000000000041d630 in cpp_peek_token (pfile=pfile@entry=0x66f560,
index=index@entry=0) at ../../src/libcpp/lex.c:1986
#3  0x000000000040e315 in peek (r=<optimized out>) at
../../src/gcc/genmatch.c:1820
#4  parse_if (r=r@entry=0x66f560, simplifiers=...) at
../../src/gcc/genmatch.c:2176
#5  0x000000000040de30 in parse_pattern (r=0x66f560, simplifiers=...)
at ../../src/gcc/genmatch.c:2207
#6  0x0000000000407738 in main (argc=6747488, argv=0x68b350) at
../../src/gcc/genmatch.c:2275

This patch fixes the seg-fault by explicitly checking for EOF in parse_if
and parse_for, however I am not sure if this is the right approach to fix it.
I suppose parse_pattern should give an error here,
since it expects CPP_OPEN_PAREN but receives CPP_EOF.

* genmatch.c (parse_if): Put check for EOF.
     (parse_for): Likewise.

Thanks,
Prathamesh

Comments

Richard Biener Aug. 8, 2014, 12:15 p.m. UTC | #1
On Fri, Aug 8, 2014 at 11:50 AM, Prathamesh Kulkarni
<bilbotheelffriend@gmail.com> wrote:
> Put check for EOF in parse_for and parse_if,
> else it results in segmentation fault.
>
> eg:
> (if (0)
>   <eof>
>
> Not sure why it segfaults.

Hmm, I think this is a bug in libcpp which seems to
crash when you do cpp_peek_token (r, 0); cpp_peek_token (r, 0);
with the first peek returning a CPP_EOF token.  The first peek
then resets r->buffer via

#0  _cpp_pop_buffer (pfile=0x67e560)
    at /space/rguenther/src/svn/match-and-simplify/libcpp/directives.c:2575
#1  0x0000000000423011 in _cpp_get_fresh_line (pfile=0x67e560)
    at /space/rguenther/src/svn/match-and-simplify/libcpp/lex.c:2146
#2  0x00000000004230f1 in _cpp_lex_direct (pfile=0x67e560)
    at /space/rguenther/src/svn/match-and-simplify/libcpp/lex.c:2193
#3  0x0000000000422e05 in _cpp_lex_token (pfile=0x67e560)
    at /space/rguenther/src/svn/match-and-simplify/libcpp/lex.c:2067
#4  0x0000000000422ac0 in cpp_peek_token (pfile=0x67e560, index=0)
    at /space/rguenther/src/svn/match-and-simplify/libcpp/lex.c:1986

It seems that cpp_peek_token (r, 10) and hitting EOF somewhere
inbetween would show the same behavior.

Tom?

Richard.

> I got following back-trace (in gdb):
>
> #0  _cpp_lex_direct (pfile=pfile@entry=0x66f560) at ../../src/libcpp/lex.c:2183
> #1  0x000000000041d50c in _cpp_lex_token (pfile=pfile@entry=0x66f560)
> at ../../src/libcpp/lex.c:2067
> #2  0x000000000041d630 in cpp_peek_token (pfile=pfile@entry=0x66f560,
> index=index@entry=0) at ../../src/libcpp/lex.c:1986
> #3  0x000000000040e315 in peek (r=<optimized out>) at
> ../../src/gcc/genmatch.c:1820
> #4  parse_if (r=r@entry=0x66f560, simplifiers=...) at
> ../../src/gcc/genmatch.c:2176
> #5  0x000000000040de30 in parse_pattern (r=0x66f560, simplifiers=...)
> at ../../src/gcc/genmatch.c:2207
> #6  0x0000000000407738 in main (argc=6747488, argv=0x68b350) at
> ../../src/gcc/genmatch.c:2275
>
> This patch fixes the seg-fault by explicitly checking for EOF in parse_if
> and parse_for, however I am not sure if this is the right approach to fix it.
> I suppose parse_pattern should give an error here,
> since it expects CPP_OPEN_PAREN but receives CPP_EOF.
>
> * genmatch.c (parse_if): Put check for EOF.
>      (parse_for): Likewise.
>
> Thanks,
> Prathamesh
diff mbox

Patch

Index: genmatch.c
===================================================================
--- genmatch.c	(revision 213752)
+++ genmatch.c	(working copy)
@@ -2137,7 +2137,9 @@  parse_for (cpp_reader *r, source_locatio
       const cpp_token *token = peek (r);
       if (token->type == CPP_CLOSE_PAREN)
 	break;
-      
+      else if (token->type == CPP_EOF)
+	fatal_at (token, "missing ending ')' in for");      
+
       vec<simplify *> for_simplifiers = vNULL;
       parse_pattern (r, for_simplifiers);
       
@@ -2174,6 +2176,8 @@  parse_if (cpp_reader *r, vec<simplify *>
       const cpp_token *token = peek (r);
       if (token->type == CPP_CLOSE_PAREN)
 	break;
+      else if (token->type == CPP_EOF)
+	fatal_at (token, "missing ending ')' in if");      
     
       parse_pattern (r, simplifiers);
     }