[gnu.utils.bug] unportable use of malloc in ld.c

ham@polya.Stanford.EDU (Peter R. Ham) (06/06/89)

I don't have a version number for this code, but the last date in the
ChangeLog is:

Thu Dec 29 01:48:03 1988  Richard Stallman  (rms at sugar-bombs.ai.mit.edu)

The following code implicity assumes that xmalloc() returns zeroed memory:

main()
...
  cmdline_references =
    (struct glosym **) xmalloc (cl_refs_allocated
				* sizeof(struct glosym *));

It's my understanding that "malloc()", which "xmalloc()" calls is not
guarranteed return a pointer to zero filled memory.

This dependecny can be fixed by clearing the memory with bzero inside xmalloc()
or appending:

	bzero((char *) cmdline_references, cl_refs_allocated * 
		sizeof(struct glosym *));

After the call to xmalloc.


Later on, this assumption is used:


void
add_cmdline_ref (sp)
     struct glosym *sp;
{
  struct glosym **ptr;

  for (ptr = cmdline_references;
       ptr < cmdline_references + cl_refs_allocated && *ptr;(RIGHT HERE!!!)
       ptr++)
    ;

  if (ptr == cmdline_references + cl_refs_allocated)
    {
      int diff = ptr - cmdline_references;
      
      cl_refs_allocated *= 2;
      cmdline_references = (struct glosym **)
	xrealloc (cmdline_references,
		 cl_refs_allocated * sizeof (struct glosym *));
      ptr = cmdline_references + diff;
    }
  
  *ptr++ = sp;
  *ptr = (struct glosym *) 0;
}


and there is similar code in "do_warnings()".



--
Peter Ham			PO Box 3430	(415) 324-4782
MS Computer Science Student	Stanford, CA	ham@polya.stanford.edu
Stanford University 		94309