All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix segfault in dtc when empty input file is given.
@ 2012-09-14 22:23 Markus Mayer
  2012-09-25 15:44 ` Stephen Warren
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Mayer @ 2012-09-14 22:23 UTC (permalink / raw)
  To: linux-kernel

From: Markus Mayer <mmayer@broadcom.com>
Date: Thu, 13 Sep 2012 14:46:28 -0700
Subject: [PATCH] Fix segfault in dtc when empty input file is given.

Prior to this change, an empty input file would cause a segfault, because
yylloc had never been initialized. There was never any characters for the
lexer to match, so YY_USER_ACTION was never executed before the parse error
was detected.

When the parser printed the error message, it tried to include the name of
the file, but the structure holding the file name (yylloc.file, referenced
as pos->file) had never been initialized.

Without the fix:

$ ./dtc /dev/null
DTC: dts->dts  on file "/dev/null"
Segmentation fault (core dumped)

$ gdb dtc core
Program terminated with signal 11, Segmentation fault.
    at scripts/dtc/srcpos.c:194
194			fname = pos->file->name;
(gdb) bt
    at scripts/dtc/srcpos.c:194
    fmt=0x40d769 "%s", va=0x7fffbf027148) at scripts/dtc/srcpos.c:220
    at scripts/dtc/dtc-parser.tab.c:1920
    at scripts/dtc/treesource.c:38
    at scripts/dtc/dtc.c:203
(gdb) p *pos
$1 = {first_line = 0, first_column = 0, last_line = 0, last_column = 0,
  file = 0x0}

With the fix:

$ ./dtc /dev/null
DTC: dts->dts  on file "/dev/null"
Error: /dev/null:1.1 syntax error
FATAL ERROR: Unable to parse input tree

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
 scripts/dtc/treesource.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/scripts/dtc/treesource.c b/scripts/dtc/treesource.c
index c09aafa..b461b88 100644
--- a/scripts/dtc/treesource.c
+++ b/scripts/dtc/treesource.c
@@ -29,11 +29,15 @@ int treesource_error;
 
 struct boot_info *dt_from_source(const char *fname)
 {
+	extern YYLTYPE yylloc;
+
 	the_boot_info = NULL;
 	treesource_error = 0;
 
 	srcfile_push(fname);
 	yyin = current_srcfile->f;
+	/* Initialize yylloc->file to avoid segfault on empty input */
+	srcpos_update(&yylloc, NULL, 0);
 
 	if (yyparse() != 0)
 		die("Unable to parse input tree\n");
-- 
1.7.5.4


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

* Re: [PATCH] Fix segfault in dtc when empty input file is given.
  2012-09-14 22:23 [PATCH] Fix segfault in dtc when empty input file is given Markus Mayer
@ 2012-09-25 15:44 ` Stephen Warren
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Warren @ 2012-09-25 15:44 UTC (permalink / raw)
  To: Markus Mayer; +Cc: linux-kernel

On 09/14/2012 04:23 PM, Markus Mayer wrote:
> Prior to this change, an empty input file would cause a segfault, because
> yylloc had never been initialized. There was never any characters for the
> lexer to match, so YY_USER_ACTION was never executed before the parse error
> was detected.
> 
> When the parser printed the error message, it tried to include the name of
> the file, but the structure holding the file name (yylloc.file, referenced
> as pos->file) had never been initialized.

> diff --git a/scripts/dtc/treesource.c b/scripts/dtc/treesource.c

>  struct boot_info *dt_from_source(const char *fname)
>  {
> +	extern YYLTYPE yylloc;
> +
>  	the_boot_info = NULL;
>  	treesource_error = 0;
>  
>  	srcfile_push(fname);
>  	yyin = current_srcfile->f;
> +	/* Initialize yylloc->file to avoid segfault on empty input */
> +	srcpos_update(&yylloc, NULL, 0);

Wouldn't srcpos_update(&yyloc, fname, 0); be even more informative?


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

end of thread, other threads:[~2012-09-25 15:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-14 22:23 [PATCH] Fix segfault in dtc when empty input file is given Markus Mayer
2012-09-25 15:44 ` Stephen Warren

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.