This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: [PATCH] memcmp() error in gas/dwarf2dbg.c
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Andreas Jaeger <aj at suse dot de>
- Cc: Hannes Reinecke <hare at suse dot de>, binutils at sources dot redhat dot com, Martin Schwidefsky <schwidefsky at de dot ibm dot com>, Ulrich Weigand <Ulrich dot Weigand at de dot ibm dot com>
- Date: Fri, 13 Feb 2004 10:32:44 +0100
- Subject: Re: [PATCH] memcmp() error in gas/dwarf2dbg.c
- References: <402CAF31.7010701@suse.de> <m3u11vdy2t.fsf@gromit.moeb>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Fri, Feb 13, 2004 at 12:21:30PM +0100, Andreas Jaeger wrote:
> > --- binutils-2.14.90.0.8/gas/dwarf2dbg..c.orig 2004-02-13 11:55:05.470239719 +0100
> > +++ binutils-2.14.90.0.8/gas/dwarf2dbg.c 2004-02-13 11:57:23.679576129 +0100
>
> Note this patch applies also to current CVS, Hannes noticed it with a
> segmentation fault of gas due to accessing beyond the allocated memory.
>
> > @@ -339,7 +339,7 @@ get_filenum (const char *filename, unsig
> > {
> > static unsigned int last_used, last_used_dir_len;
> > const char *file;
> > - size_t dir_len;
> > + size_t dir_len, tmp_len;
> > unsigned int i, dir;
> >
> > if (num == 0 && last_used)
> > @@ -372,8 +372,9 @@ get_filenum (const char *filename, unsig
> > {
> > --dir_len;
> > for (dir = 1; dir < dirs_in_use; ++dir)
> > - if (memcmp (filename, dirs[dir], dir_len) == 0
> > - && dirs[dir][dir_len] == '\0')
> > + tmp_len = strlen(dirs[dir]) < dir_len?strlen(dirs[dir]):dir_len;
> > + if (memcmp (filename, dirs[dir], tmp_len) == 0
> > + && dirs[dir][tmp_len] == '\0')
> > break;
> >
> > if (dir >= dirs_in_use)
I very much doubt it has been tested at all.
Previously it has been doing if (...) break; in the for cycle, now
it does just tmp_len = in the for cycle and if (...) break; afterwards.
Also, calling strlen twice is a waste, relying on compiler to optimize
it out is wrong. And doing even one strlen in the cycle is too expensive.
I believe using
strncmp (filename, dirs[dir], dir_len) == 0 && dirs[dir][dir_len] == '\0'
would be much better.
Jakub