Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Jun 1997 06:10:02 -0700 (PDT)
From:      Tim Vanderhoek <tim@X2296>
To:        freebsd-bugs
Subject:   Re: bin/3451: vasprintf() doesn't work.
Message-ID:  <199706141310.GAA22358@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/3451; it has been noted by GNATS.

From: Tim Vanderhoek <tim@X2296>
To: Bruce Evans <bde@zeta.org.au>
Cc: ac199@hwcn.org, freebsd-bugs@hub.freebsd.org,
        freebsd-gnats-submit@freebsd.org
Subject: Re: bin/3451: vasprintf() doesn't work.
Date: Sat, 14 Jun 1997 09:07:46 -0400 (EDT)

 On Sat, 14 Jun 1997, Bruce Evans wrote:
 
 > >   	*str = realloc(h.base, (size_t)(h.size - h.left + 1));
 > >   	if (*str == NULL)	/* failed to realloc it to actual size */
 > > ! 		return (-1);
 > > ! 	(*str)[h.size - h.left] = '\0';
 > >   	return (ret);
 > >   }
 > 
 > One more problem: realloc() can fail, resulting in a leaking the memory
 > pointed to by h.base.
 
 Which, of course, points us to yet another error in the original
 vasprintf.c...  :)  The test to see if str is NULL is
 *str == NULL....
 
 Final patch frees h.base (as is done everywhere else) and fixes 
 that... :)
 
 
 *** old.vasprintf.c	Tue Apr 29 21:32:00 1997
 --- vasprintf.c	Sat Jun 14 08:56:41 1997
 ***************
 *** 111,119 ****
   	if (h.base == NULL)	/* failed to realloc in writehook */
   		return (-1);
   
 - 	h.base[h.size - h.left] = '\0';
   	*str = realloc(h.base, (size_t)(h.size - h.left + 1));
 ! 	if (*str == NULL)	/* failed to realloc it to actual size */
 ! 		*str = h.base;	/* return oversize buffer */
   	return (ret);
   }
 --- 111,121 ----
   	if (h.base == NULL)	/* failed to realloc in writehook */
   		return (-1);
   
   	*str = realloc(h.base, (size_t)(h.size - h.left + 1));
 ! 	if (str == NULL) {	/* failed to realloc it to actual size */
 ! 		free(h.base);
 ! 		return (-1);
 ! 	}
 ! 	(*str)[h.size - h.left] = '\0';
   	return (ret);
   }
 
 
 --
 tIM...HOEk
 optimization: The theory that making your code incomprehensible by using
               only one-letter variable names will make it run faster.
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199706141310.GAA22358>