ld parser buffer leak

Alan Modra amodra@gmail.com
Fri Jan 17 03:52:40 GMT 2025


On Thu, Jan 16, 2025 at 10:29:59PM +0000, Sam James wrote:
> Sam James <sam@gentoo.org> writes:
> 
> > Alan Modra <amodra@gmail.com> writes:
> >
> >> 	* ldlex.l (<<EOF>>): yy_delete_buffer current.
> >> 	(yy_create_string_buffer): Use yyalloc.
> >>
> >> diff --git a/ld/ldlex.l b/ld/ldlex.l
> >> index ef0c5bdf425..f36362fefc1 100644
> >> --- a/ld/ldlex.l
> >> +++ b/ld/ldlex.l
> >> @@ -477,6 +477,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
> >>  				}
> >>  
> >>  <<EOF>> {
> >> +  yy_delete_buffer (YY_CURRENT_BUFFER);
> >>    include_stack_ptr--;
> >>    if (include_stack_ptr == 0)
> >>      {
> >> @@ -528,13 +529,13 @@ yy_create_string_buffer (const char *string, size_t size)
> >>  {
> >>    YY_BUFFER_STATE b;
> >>  
> >> -  b = xmalloc (sizeof (struct yy_buffer_state));
> >> +  b = yyalloc (sizeof (struct yy_buffer_state));
> >>    b->yy_input_file = 0;
> >>    b->yy_buf_size = size;
> >>  
> >>    /* yy_ch_buf has to be 2 characters longer than the size given because
> >>       we need to put in 2 end-of-buffer characters.  */
> >> -  b->yy_ch_buf = xmalloc ((size_t) b->yy_buf_size + 3);
> >> +  b->yy_ch_buf = yyalloc (b->yy_buf_size + 3);
> >>  
> >>    b->yy_ch_buf[0] = '\n';
> >>    strcpy (b->yy_ch_buf+1, string);
> >
> > In file included from /var/tmp/portage/sys-devel/binutils-9999/work/binutils/ld/ldlex-wrapper.c:26:
> > /var/tmp/portage/sys-devel/binutils-9999/work/binutils/ld/ldlex.l: In function ‘yy_create_string_buffer’:
> > /var/tmp/portage/sys-devel/binutils-9999/work/binutils/ld/ldlex.l:532:7: error: implicit declaration of function ‘yyalloc’; did you mean ‘xcalloc’? [-Wimplicit-function-declaration]
> >   532 |   b = yyalloc (sizeof (struct yy_buffer_state));
> >       |       ^~~~~~~
> >       |       xcalloc
> > /var/tmp/portage/sys-devel/binutils-9999/work/binutils/ld/ldlex.l:532:5:
> > error: assignment to ‘YY_BUFFER_STATE’ {aka ‘struct yy_buffer_state
> > *’} from ‘int’ makes pointer from integer without a cast
> > [-Wint-conversion]
> >   532 |   b = yyalloc (sizeof (struct yy_buffer_state));
> >       |     ^
> > /var/tmp/portage/sys-devel/binutils-9999/work/binutils/ld/ldlex.l:538:16: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
> >   538 |   b->yy_ch_buf = yyalloc (b->yy_buf_size + 3);
> >       |                ^
> > make[4]: *** [Makefile:2297: ldlex-wrapper.o] Error 1
> 
> Looks like reflex (Thomas Dickey's) doesn't have yyalloc, just flex
> does.

Huh, and here I was thinking it would be better to use yyalloc since
it is possible to override the default alloc/realloc/free functions.

I'll put them back to xmalloc as I don't care enough to investigate
any better solution.

-- 
Alan Modra


More information about the Binutils mailing list