[PATCH] memcmp() error in gas/dwarf2dbg.c
Jakub Jelinek
jakub@redhat.com
Fri Feb 13 11:41:00 GMT 2004
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
More information about the Binutils
mailing list