diff mbox

preprocessor/58580 - preprocessor goes OOM with warning for zero literals

Message ID 878uu5qs3q.fsf@redhat.com
State New
Headers show

Commit Message

Dodji Seketeli Jan. 24, 2014, 4:09 p.m. UTC
Jakub Jelinek <jakub@redhat.com> writes:

> On Fri, Jan 24, 2014 at 04:40:52PM +0100, Dodji Seketeli wrote:
>> > The patch causes http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59935 .
>> > The follow-up patch (fp == NULL check) doesn't help.
>> 
>> I am looking into that, sorry for the inconvenience.
>
> I'd say we want something like following.  Note that while the c == NULL
> bailout would be usually sufficient, if you'll do:
> echo foobar > '<command-line>'
> it would still crash.  Line 0 is used only for the special locations
> (command line, built-in macros) and there is no file associated with it
> anyway.
>
> --- gcc/input.c.jj	2014-01-24 16:32:34.000000000 +0100
> +++ gcc/input.c	2014-01-24 16:41:42.012671452 +0100
> @@ -698,7 +698,13 @@ location_get_source_line (expanded_locat
>    static char *buffer;
>    static ssize_t len;
>  
> -  fcache * c = lookup_or_add_file_to_cache_tab (xloc.file);
> +  if (xloc.line == 0)
> +    return NULL;
> +
> +  fcache *c = lookup_or_add_file_to_cache_tab (xloc.file);
> +  if (c == NULL)
> +    return NULL;
> +
>    bool read = read_line_num (c, xloc.line, &buffer, &len);
>  
>    if (read && line_len)

Indeed.

Though, I am testing the patch below that makes read_line_num gracefully
handle empty caches or zero locations.  The rest of the code should just
work with that as is.

	* input.c (read_line_num): Gracefully handle non-file locations or
	empty caches.

Comments

Jakub Jelinek Jan. 24, 2014, 4:13 p.m. UTC | #1
On Fri, Jan 24, 2014 at 05:09:29PM +0100, Dodji Seketeli wrote:
> 	* input.c (read_line_num): Gracefully handle non-file locations or
> 	empty caches.
> 
> diff --git a/gcc/input.c b/gcc/input.c
> index 547c177..b05e1da 100644
> --- a/gcc/input.c
> +++ b/gcc/input.c
> @@ -600,7 +600,8 @@ static bool
>  read_line_num (fcache *c, size_t line_num,
>  	       char ** line, ssize_t *line_len)
>  {
> -  gcc_assert (line_num > 0);
> +  if (!c || line_num < 1)
> +    return false;
>  
>    if (line_num <= c->line_num)
>      {

Ok.

> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/cpp/warning-zero-location.c
> @@ -0,0 +1,6 @@
> +/*
> +   { dg-options "-D _GNU_SOURCE" }
> +   { dg-do compile }
> + */
> +
> +#define _GNU_SOURCE 	/* { dg-warning "redefined" } */

I doubt this would fail without the patch though, because
fno-diagnostics-show-caret is added by default to flags.
So, I'd say you need also -fdiagnostics-show-caret in dg-options to
reproduce it.

	Jakub
Markus Trippelsdorf Jan. 24, 2014, 11:02 p.m. UTC | #2
On 2014.01.24 at 17:09 +0100, Dodji Seketeli wrote:
> Jakub Jelinek <jakub@redhat.com> writes:
> 
> > On Fri, Jan 24, 2014 at 04:40:52PM +0100, Dodji Seketeli wrote:
> >> > The patch causes http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59935 .
> >> > The follow-up patch (fp == NULL check) doesn't help.
> >> 
> >> I am looking into that, sorry for the inconvenience.
> >
> > I'd say we want something like following.  Note that while the c == NULL
> > bailout would be usually sufficient, if you'll do:
> > echo foobar > '<command-line>'
> > it would still crash.  Line 0 is used only for the special locations
> > (command line, built-in macros) and there is no file associated with it
> > anyway.
> >
> > --- gcc/input.c.jj	2014-01-24 16:32:34.000000000 +0100
> > +++ gcc/input.c	2014-01-24 16:41:42.012671452 +0100
> > @@ -698,7 +698,13 @@ location_get_source_line (expanded_locat
> >    static char *buffer;
> >    static ssize_t len;
> >  
> > -  fcache * c = lookup_or_add_file_to_cache_tab (xloc.file);
> > +  if (xloc.line == 0)
> > +    return NULL;
> > +
> > +  fcache *c = lookup_or_add_file_to_cache_tab (xloc.file);
> > +  if (c == NULL)
> > +    return NULL;
> > +
> >    bool read = read_line_num (c, xloc.line, &buffer, &len);
> >  
> >    if (read && line_len)
> 
> Indeed.
> 
> Though, I am testing the patch below that makes read_line_num gracefully
> handle empty caches or zero locations.  The rest of the code should just
> work with that as is.
> 
> 	* input.c (read_line_num): Gracefully handle non-file locations or
> 	empty caches.

Unfortunately this doesn't fix yet another issue:

markus@x4 /tmp % cat foo.c
#line 4636 "configure"
#include <xxxxxxxxxxxx.h>
int main() { return 0; }

markus@x4 /tmp % gcc foo.c
configure:4636:26: fatal error: xxxxxxxxxxxx.h: No such file or directory
gcc: internal compiler error: Segmentation fault (program cc1)
0x40cc8e execute
        ../../gcc/gcc/gcc.c:2841
0x40cf09 do_spec_1
        ../../gcc/gcc/gcc.c:4641
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
0x40d692 do_spec_1
        ../../gcc/gcc/gcc.c:5295
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
0x40d692 do_spec_1
        ../../gcc/gcc/gcc.c:5295
0x40d28e do_spec_1
        ../../gcc/gcc/gcc.c:5410
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
0x40d692 do_spec_1
        ../../gcc/gcc/gcc.c:5295
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
0x40d692 do_spec_1
        ../../gcc/gcc/gcc.c:5295
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
0x40d692 do_spec_1
        ../../gcc/gcc/gcc.c:5295
0x40fc91 process_brace_body
        ../../gcc/gcc/gcc.c:5924
0x40fc91 handle_braces
        ../../gcc/gcc/gcc.c:5838
Please submit a full bug report,
with preprocessed source if appropriate.
Markus Trippelsdorf Jan. 24, 2014, 11:20 p.m. UTC | #3
On 2014.01.25 at 00:02 +0100, Markus Trippelsdorf wrote:
> On 2014.01.24 at 17:09 +0100, Dodji Seketeli wrote:
> > Jakub Jelinek <jakub@redhat.com> writes:
> > 
> > > On Fri, Jan 24, 2014 at 04:40:52PM +0100, Dodji Seketeli wrote:
> > >> > The patch causes http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59935 .
> > >> > The follow-up patch (fp == NULL check) doesn't help.
> > >> 
> > >> I am looking into that, sorry for the inconvenience.
> > >
> > > I'd say we want something like following.  Note that while the c == NULL
> > > bailout would be usually sufficient, if you'll do:
> > > echo foobar > '<command-line>'
> > > it would still crash.  Line 0 is used only for the special locations
> > > (command line, built-in macros) and there is no file associated with it
> > > anyway.
> > >
> > > --- gcc/input.c.jj	2014-01-24 16:32:34.000000000 +0100
> > > +++ gcc/input.c	2014-01-24 16:41:42.012671452 +0100
> > > @@ -698,7 +698,13 @@ location_get_source_line (expanded_locat
> > >    static char *buffer;
> > >    static ssize_t len;
> > >  
> > > -  fcache * c = lookup_or_add_file_to_cache_tab (xloc.file);
> > > +  if (xloc.line == 0)
> > > +    return NULL;
> > > +
> > > +  fcache *c = lookup_or_add_file_to_cache_tab (xloc.file);
> > > +  if (c == NULL)
> > > +    return NULL;
> > > +
> > >    bool read = read_line_num (c, xloc.line, &buffer, &len);
> > >  
> > >    if (read && line_len)
> > 
> > Indeed.
> > 
> > Though, I am testing the patch below that makes read_line_num gracefully
> > handle empty caches or zero locations.  The rest of the code should just
> > work with that as is.
> > 
> > 	* input.c (read_line_num): Gracefully handle non-file locations or
> > 	empty caches.
> 
> Unfortunately this doesn't fix yet another issue:

Whereas Jakub's patch is fine.
diff mbox

Patch

diff --git a/gcc/input.c b/gcc/input.c
index 547c177..b05e1da 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -600,7 +600,8 @@  static bool
 read_line_num (fcache *c, size_t line_num,
 	       char ** line, ssize_t *line_len)
 {
-  gcc_assert (line_num > 0);
+  if (!c || line_num < 1)
+    return false;
 
   if (line_num <= c->line_num)
     {
diff --git a/gcc/testsuite/c-c++-common/cpp/warning-zero-location.c b/gcc/testsuite/c-c++-common/cpp/warning-zero-location.c
new file mode 100644
index 0000000..04a06b2
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/warning-zero-location.c
@@ -0,0 +1,6 @@ 
+/*
+   { dg-options "-D _GNU_SOURCE" }
+   { dg-do compile }
+ */
+
+#define _GNU_SOURCE 	/* { dg-warning "redefined" } */