bug in the 'ro' text formatter

Jim Levie jim at chimera.UUCP
Sat Mar 3 05:48:52 AEST 1990


jik at athena.mit.edu (Jonathan I. Kamens) writes:

>In article <1126 at laas.laas.fr>, rlacoste at kebra.laas.fr (Robert Lacoste) writes:
>> 	Proposed correction:
>> 		Replacement of all strlen calls by mystrlen, with the
>> 		following definition:
>> 
>> 			int mystrlen(s)
>> 			char *s;
>> 			{
>> 			  if (s==NULL) return (0);
>> 			  else return(strlen(s));
>> 			}

>... stuff deleted ...

>  Instead of what you've proposed, I suggest this alternative:

>    #define mystrlen(s) ((s) ? strlen(s) : 0)

Uh, you really don't want to do any of the above.  The problem is that ro is
trying to execute the ".ds" instruction, and for that it needs a place for
the string name and the string.  The posted code either used an existing
"insertion structure" (when the string register was being redefined), or
created a new "insertion structure" (with a NULL string pointer). It then
checked to see if the new string was larger than the old string, which was
fine as long as there was an old string, but which won't work when the string
register is being used for the first time (the string pointer is NULL).

I believe that the correct fix is something like the following code.  I
"indented" it so I could read it easier, so I can't do a diff.

>From insert() in ro_macr.c

    /*
     * see if it is already defined
     */
    if((sptr = find2(name, slink))!=NULL)	
    {
	if(ro_verbose==TRUE)
	{
	    fprintf(stderr, "%cWarning: <%s> was defined to be <%s>\n",
		    BELL, name, sptr->mstr);
	    fprintf(stderr, "...now it is defined to be <%s>\n", tbuf);
	}
	/*
	 * allocate memory for the new string if it is longer 
	 * than the current string
	 */ 
	if(strlen(tbuf)>strlen(sptr->mstr))
	{
	    if((sptr->mstr = malloc((size_t)strlen(tbuf)+1))==NULL)
	    {
		fprintf(stderr,
			"Fatal: failed to allocate memory for string.\n");
		exit(-1);
	    }
	}
    }
    else 
    {
	if((sptr = (struct divfd *)malloc((size_t)sizeof(struct divfd)))==NULL)
	{
	    fprintf(stderr,
		    "Fatal: cannot allocate memory for insertion structure.\n"); 
	    exit(-1);
	}
	
	/*
	 * Allocation succeeded, set up links,  then allocate space for
	 * the string to be copied into
	 */
	sptr->prev = slink;		/* save previous link	*/
	slink = sptr;			/* reset link		*/
	strcpy(sptr->nm, name);
	sptr->mstr = NULL;
	if((sptr->mstr = malloc((size_t)strlen(tbuf)+1))==NULL)
	{
	    fprintf(stderr,
		    "Fatal: failed to allocate memory for string.\n");
	    exit(-1);
	}
    }
    
    /*
     * copy the string to the new memory
     */
    strcpy(sptr->mstr, tbuf);
}


-- 
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 Jim Levie   REMTECH Inc  Huntsville, Al 
 The opinions expressed above are just that.
 Ph.    (205) 536-8581               email: uunet!ingr!chimera!jim



More information about the Comp.sources.bugs mailing list