[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [leafnode-list] Fetchnews Problem



krasel@xxxxxxxxxxxxxxxxxxxxxxxxxxxx (Cornelius Krasel) writes:

> char *getaline(FILE *f) {
>     static char *buf;       /* buffer for line */
>     static size_t size;     /* size of buffer */
>     size_t len;             /* # of chars stored into buf before '\0' */
>     char * p;
>     int i;
> 
>     len = 0;
>     if (!size)
> 	size = 256;
>     if (!buf)
> 	buf = critmalloc( size, "reading line" );
> 
>     while ((p=fgets(buf+len, size-len, f))) {
> 	/* replace NUL bytes (which shouldn't be there anyway)
> 	 * with spaces
> 	 */
> 	i = size - len;
> 	while ( *p++ != '\n' && i-- > 0 ) {
> 	    if ( *p == '\0' )
> 		*p = ' ';
> 	}
>	len += strlen(buf+len);

leafnode dies if the buffer is too short, plus this cannot fix if the
first byte is a NUL, for p++ is executed before the loop body is
entered. 

Besides, if there is no "\n", this loop will nuke the final \0, making
the subsequent strlen return bogus values (may be too high). 

I'm successfully using this loop (note --i instead of i-- and p++ moved
into the loop body). 

	while ( *p != '\n' && --i > 0 ) {
	    if ( *p == '\0' )
		*p = ' ';
	    p++;
	}

Will fix this in 1.9.16ma3. 

-- 
Matthias Andree

                Where do you think you're going today?

-- 
leafnode-list@xxxxxxxxxxxxxxxxxxxxxxxxxxxx -- mailing list for leafnode
To unsubscribe, send mail with "unsubscribe" in the subject to the list