Subject: lib/16483: memory leak in snprintf and vsnprintf
To: None <>
From: Brad Knotwell <>
List: netbsd-bugs
Date: 04/24/2002 11:01:37
>Number:         16483
>Category:       lib
>Synopsis:       memory leak in snprintf and vsnprintf
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Apr 24 10:59:00 PDT 2002
>Originator:     Brad Knotwell
>Release:        NetBSD 1.5.1
System: NetBSD netbsdws 1.5.3_RC1 NetBSD 1.5.3_RC1 (BRAD) #0: Wed Mar 13 09:30:36 PST 2002 root@netbsdws:/usr/src/sys/arch/i386/compile/BRAD i386


snprintf and vsnprintf should take an argument like the following:

    snprintf(NULL,(size_t)0,"%s %s %s","arg1","arg2","arg3");

so someone can determine how much memory to allocate for a string.
Unfortunately, this leaks memory.

#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
int main(int argc,char **argv)
    int buflen;
    for( ; ; ) buflen = snprintf(NULL,(size_t)0,"%s %s %s","brad","was","here");


Not sure.  FreeBSD4.5 has an identical problem.  BSD/OS 4.1 doesn't.
I don't have access to an OpenBSD machine.