From tim@X2296  Wed Apr 30 02:19:07 1997
Received: from X2296 (ppp1584.on.sympatico.ca [206.172.249.48])
          by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id CAA17567
          for <FreeBSD-gnats-submit@freebsd.org>; Wed, 30 Apr 1997 02:19:05 -0700 (PDT)
Received: (from tim@localhost) by X2296 (8.7.6/8.7.3) id FAA00373; Wed, 30 Apr 1997 05:18:46 -0400 (EDT)
Message-Id: <199704300918.FAA00373@X2296>
Date: Wed, 30 Apr 1997 05:18:46 -0400 (EDT)
From: Tim.Vanderhoek@X2296
Reply-To: ac199@hwcn.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: vasprintf() doesn't work
X-Send-Pr-Version: 3.2

>Number:         3443
>Category:       bin
>Synopsis:       vasprintf() doesn't work
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May  1 00:53:53 PDT 1997
>Closed-Date:    Thu May 1 04:04:42 PDT 1997
>Last-Modified:  Thu May  1 04:05:46 PDT 1997
>Originator:     Tim Vanderhoek
>Release:        FreeBSD 2.2-961006-SNAP i386
>Organization:
League of the Missing
>Environment:

vasprintf.c from -current as of Apr. 29.

This is actually the second time sending this pr, since the first one
seems to have gotten lost.  Apologies if it suddenly appears...

>Description:

It's a simple off-by-one error...  It rears it's head when the length
of the string to be printed is such that h.left == 0 after vprintf().

Recall from line #69 of vasprintf.c that h.base is allocatted the
following space:  newbuf = realloc(h->base, h->size);

However, on line #116 of vasprintf.c, where h.left = 0, we
do:  h.base[h.size - 0] = '\0';

This is not good, because it can do bad things such as causing learning
curves in inexperienced hackers such as me, or make static local strings
in distant functions disappear.

>How-To-Repeat:

Who cares?

>Fix:
	
*** old.vasprintf.c	Tue Apr 29 21:32:00 1997
--- vasprintf.c	Tue Apr 29 21:31:45 1997
***************
*** 111,118 ****
  	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,118 ----
  	if (h.base == NULL)	/* failed to realloc in writehook */
  		return (-1);
  
  	*str = realloc(h.base, (size_t)(h.size - h.left + 1));
+ 	(*str)[h.size - h.left] = '\0';
  	if (*str == NULL)	/* failed to realloc it to actual size */
  		*str = h.base;	/* return oversize buffer */
  	return (ret);
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: peter 
State-Changed-When: Thu May 1 04:04:42 PDT 1997 
State-Changed-Why:  
Duplicate of PR due to the gnats blockage. 
>Unformatted:
