Subject: standards/9603: snprintf(0, 0, ...) is valid in C9X
To: None <gnats-bugs@gnats.netbsd.org>
From: Seebs <seebs@ged.plethora.net>
List: netbsd-bugs
Date: 03/11/2000 17:15:47
>Number:         9603
>Category:       standards
>Synopsis:       snprintf(0, 0, ...) should succeed
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    standards-manager (NetBSD Standards Conformance bug manager)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Mar 11 17:15:00 2000
>Last-Modified:
>Originator:     
>Organization:
>Release:        any
>Environment:
System: NetBSD ged.plethora.net 1.4T NetBSD 1.4T (LOBE) #0: Fri Feb 25 12:07:10 CST 2000 seebs@ged.plethora.net:/usr/src/sys/arch/i386/compile/LOBE i386


>Description:
	snprintf(buffer, 0, fmt...) is a supported/standard feature of C9X;
	it does exactly what you'd naively "expect" it to do, which is write
	at most 0 characters into 'buffer', and return the number of
	characters it would have had to write to produce the full string.

	Furthermore, *if* n is 0, buffer may be a null pointer, and you still
	get back the number you should have gotten.

>How-To-Repeat:
	Read the C9X spec.

>Fix:

It turns out that we already handle a null buffer as long as we don't try
to write to it, so all we seem to need is the following two patches to allow
the 0 size.

*** snprintf.c.orig	Sat Mar 11 19:01:37 2000
--- snprintf.c	Sat Mar 11 19:03:22 2000
***************
*** 78,84 ****
  	_DIAGASSERT(str != NULL);
  	_DIAGASSERT(fmt != NULL);
  
! 	if ((int)n < 1)
  		return (-1);
  #if __STDC__
  	va_start(ap, fmt);
--- 78,84 ----
  	_DIAGASSERT(str != NULL);
  	_DIAGASSERT(fmt != NULL);
  
! 	if ((int)n < 0)
  		return (-1);
  #if __STDC__
  	va_start(ap, fmt);
*** vsnprintf.c.orig	Sat Mar 11 19:08:07 2000
--- vsnprintf.c	Sat Mar 11 19:08:13 2000
***************
*** 68,74 ****
  	_DIAGASSERT(str != NULL);
  	_DIAGASSERT(fmt != NULL);
  
! 	if ((int)n < 1)
  		return (-1);
  	f._file = -1;
  	f._flags = __SWR | __SSTR;
--- 68,74 ----
  	_DIAGASSERT(str != NULL);
  	_DIAGASSERT(fmt != NULL);
  
! 	if ((int)n < 0)
  		return (-1);
  	f._file = -1;
  	f._flags = __SWR | __SSTR;
>Audit-Trail:
>Unformatted: