[net.emacs] emacs #264 can't do filename completion in big dirs.

matt@oddjob.UChicago.UUCP (Matt Crawford) (06/10/84)

Index:	filecomp.c FIX

Description:
	If the total length of filenames in the directory you attempt
	completion in (excluding .o files, backups, etc) is more than about
	1000 characters, filename completion does not work.  If, in addition,
	there are over 50 files, you probably get a core dump.
Repeat-By:
	Should be obvious.  For me it happens in the emacs source directory.
Fix:
	When a realloc() of the string space used to hold filenames is done
	the pointers in DirEnts[] must be updated to point to the new
	EntryNames[] space.  If DirEnts[] is later realloc()'d it will wind
	up using some of the old EntryNames[] space.  In this case, MarkDir()
	will be smashing bytes in DirEnts[].
	
	The following change fixes the problem:


o 3% diff -c filecomp.c.OLD filecomp.c
*** filecomp.c.OLD
--- filecomp.c
***************
*** 181,188
  		EntryNameSize = (BytesUsed + p -> d_namlen) * 3 / 2;
  		if (EntryNameSize < 1000)
  		    EntryNameSize = 1000;
! 		EntryNames = EntryNames ? (char *) realloc (EntryNames, EntryNameSize)
! 		    : (char *) malloc (EntryNameSize);
  	    }
  	    if (DirUsed >= DirSize) {
  		DirSize = DirUsed + 50;

--- 181,201 -----
  		EntryNameSize = (BytesUsed + p -> d_namlen) * 3 / 2;
  		if (EntryNameSize < 1000)
  		    EntryNameSize = 1000;
! 		if ( EntryNames ) {
! 		/* if we realloc() must update existing pointers in DirEnts */
! 		    register int  offset;
! 		    register char *oldptr = EntryNames;
! 
! 		    EntryNames = (char *) realloc (EntryNames, EntryNameSize);
! 		    offset = EntryNames - oldptr;
! 		    if ( offset ) {
! 			register int i;
! 
! 			for ( i = 0; i < DirUsed; i++ )
! 			    DirEnts[i] += offset;
! 		    }
! 		} else		/* this is initial allocation */
! 		    EntryNames =  (char *) malloc (EntryNameSize);
  	    }
  	    if (DirUsed >= DirSize) {
  		DirSize = DirUsed + 50;
___________________________________________________________
Matt		University	ARPA: crawford@anl-mcs.arpa
Crawford	of Chicago	UUCP: ihnp4!oddjob!matt