Skip to contentSkip to conversation

Avoid potential NULL deref in scripts/genksyms/lex.l

PATCH In review Jesper Juhl 2 participants 5 messages Jun 24, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid potential NULL deref in scripts/genksyms/lex.l

strchr() returns NULL in case the string is not found and if that happens we risk dereferencing a NULL pointer. It never hurts to check for that condition and exit normally with an error rather than crashing.

(no, the indentation is not according to CodingStyle, it's simply
following whatever else is in that file)

---

 scripts/genksyms/lex.l        |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
scripts/genksyms/lex.l +2 -0
index 5e544a0..28edc0c 100644
@@ -154,6 +154,8 @@ repeat:
 
       file = strchr(yytext, '\"')+1;
       e = strchr(file, '\"');
+      if (!file || !e)
+	exit(1);
       *e = '\0';
       cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
       cur_line = atoi(yytext+2);
#1 lore raw
Quoted 20 lines · On Sun, 24 Jun 2007 23:40:03 +0200 Jesper Juhl <[email protected]> wrote:

strchr() returns NULL in case the string is not found and if that happens we risk dereferencing a NULL pointer. It never hurts to check for that condition and exit normally with an error rather than crashing.

(no, the indentation is not according to CodingStyle, it's simply
following whatever else is in that file)

---

 scripts/genksyms/lex.l        |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
scripts/genksyms/lex.l +0 -0
index 5e544a0..28edc0c 100644

scripts/genksyms/lex.l repeat: · line 156

Quoted 3 lines
scripts/genksyms/lex.l +0 -0
@@ -154,6 +154,8 @@ repeat:
 
       file = strchr(yytext, '\"')+1;
       e = strchr(file, '\"');

If `file' can be null we'd have oopsed here.

scripts/genksyms/lex.l repeat: · line 161

Quoted 5 lines
scripts/genksyms/lex.l +2 -0
+      if (!file || !e)
+	exit(1);
       *e = '\0';
       cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
       cur_line = atoi(yytext+2);

I don't think the bug which you're fixing can occur:

^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;

has anyone reported crashes in there?

#2 lore raw
Quoted 34 lines · Andrew Morton
Quoted 20 lines · On Sun, 24 Jun 2007 23:40:03 +0200 Jesper Juhl <[email protected]> wrote:

strchr() returns NULL in case the string is not found and if that happens we risk dereferencing a NULL pointer. It never hurts to check for that condition and exit normally with an error rather than crashing.

(no, the indentation is not according to CodingStyle, it's simply
following whatever else is in that file)

---

 scripts/genksyms/lex.l        |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
scripts/genksyms/lex.l +0 -0
index 5e544a0..28edc0c 100644

scripts/genksyms/lex.l repeat: · line 156

Quoted 3 lines
scripts/genksyms/lex.l +0 -0
@@ -154,6 +154,8 @@ repeat:
 
       file = strchr(yytext, '\"')+1;
       e = strchr(file, '\"');

If `file' can be null we'd have oopsed here.

scripts/genksyms/lex.l repeat: · line 161

Quoted 5 lines
scripts/genksyms/lex.l +2 -0
+      if (!file || !e)
+     exit(1);
       *e = '\0';
       cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
       cur_line = atoi(yytext+2);
Quoted 6 lines

I don't think the bug which you're fixing can occur:

^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;

has anyone reported crashes in there?

It may indeed not be possible. Found by inspection, not by any actual observed crashes.
But does it really hurt to be defensive here? In case it can somehow be caused to fail, with my patch it'll fail a bit nicer :) It's not like it's at all speed critical code that will be hurt by that extra 'if'...

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
#3 lore raw
Quoted 13 lines · On Mon, 25 Jun 2007 00:02:03 +0200 "Jesper Juhl" <[email protected]> wrote:
Quoted 6 lines

scripts/genksyms/lex.l repeat: · line 161

Quoted 5 lines
scripts/genksyms/lex.l +2 -0
+      if (!file || !e)
+     exit(1);
       *e = '\0';
       cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
       cur_line = atoi(yytext+2);
Quoted 6 lines

I don't think the bug which you're fixing can occur:

^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;

has anyone reported crashes in there?

Quoted 6 lines

It may indeed not be possible. Found by inspection, not by any actual observed crashes.
But does it really hurt to be defensive here? In case it can somehow be caused to fail, with my patch it'll fail a bit nicer :) It's not like it's at all speed critical code that will be hurt by that extra 'if'...

We can only get NULL pointers here if the regexp is wrong or if there's a bug in the regexp parser. Getting a coredump at the failure site is much better behaviour than mysteriously exiting.

#4 lore raw
Quoted 22 lines · Andrew Morton
Quoted 13 lines · On Mon, 25 Jun 2007 00:02:03 +0200 "Jesper Juhl" <[email protected]> wrote:
Quoted 6 lines

scripts/genksyms/lex.l repeat: · line 161

Quoted 5 lines
scripts/genksyms/lex.l +2 -0
+      if (!file || !e)
+     exit(1);
       *e = '\0';
       cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
       cur_line = atoi(yytext+2);
Quoted 6 lines

I don't think the bug which you're fixing can occur:

^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;

has anyone reported crashes in there?

Quoted 6 lines

It may indeed not be possible. Found by inspection, not by any actual observed crashes.
But does it really hurt to be defensive here? In case it can somehow be caused to fail, with my patch it'll fail a bit nicer :) It's not like it's at all speed critical code that will be hurt by that extra 'if'...

Quoted 4 lines

We can only get NULL pointers here if the regexp is wrong or if there's a bug in the regexp parser. Getting a coredump at the failure site is much better behaviour than mysteriously exiting.

Fair enough. Just ignore the patch then :-)

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html