Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vsprintf - safe alternative?

185 views
Skip to first unread message

Thomas Rogg

unread,
Aug 18, 2004, 8:51:35 AM8/18/04
to
Hello NG,

in my program I use the following function:

void write_log(char *format, ...)
{
va_list arg;
char txt[512];

// Get string
va_start(arg, format);
vsprintf(txt, format, arg); // TODO: txt might overflow
va_end(arg);

// Write to stdout
if(debug_console)
write_console(debug_console, txt);
printf(txt);
}

Is there any safe alternative to vsprintf? I do not want to change the
way I am passing the parameters, because I used this function in
thousands of code lines of a project I've been writing for two years now.

Thank you,

Thomas Rogg

Christopher Benson-Manica

unread,
Aug 18, 2004, 9:53:52 AM8/18/04
to
Thomas Rogg <youdontw...@web.de> spoke thus:

> Is there any safe alternative to vsprintf? I do not want to change the
> way I am passing the parameters, because I used this function in
> thousands of code lines of a project I've been writing for two years now.

Sounds like you want vsnprintf().

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Richard Tobin

unread,
Aug 18, 2004, 9:51:37 AM8/18/04
to
In article <cfvjcj$5bv$04$1...@news.t-online.com>,
Thomas Rogg <youdontw...@web.de> wrote:

>Is there any safe alternative to vsprintf?

C99 has vsnprintf, and many non-C99 systems also provide it.

-- Richard

Peter Ammon

unread,
Aug 18, 2004, 8:48:05 PM8/18/04
to
Thomas Rogg wrote:

None in C89 AFAIK, but C99 has vsnprintf()

vsprintf(txt, sizeof txt, format, arg);

GNU's libc also has the supremely convenient but nonstandard asprintf()
and vasprintf(), which malloc() the buffer for you. Use only if
portability isn't important.

-Peter

Moonie

unread,
Aug 24, 2004, 9:17:10 PM8/24/04
to

INSTEAD OF USING VSPRINTF YOU CAN USE SNPRINTF

SNPRINTF( BUF, SIZE, FORMAT, ARGS );

IT IS FOUND IN A WINDOW ENVIRONMENT AND LINUX BUT NOT DOS.
THERE MAY BE AN OPEN SOURCE FUNCTION AVAILABLE SOMEWHERE IF YOU DO A
SEARCH IN GOOGLE.COM FOR IT.

I HOPE THIS HELPS. YOU DIDN'T SPECIFY PLATFORM AND COMPILER.

--
Moonie
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Moonie

unread,
Aug 24, 2004, 9:55:23 PM8/24/04
to

_vsnprintf or vsnprintf is lile vsprintf

snprintf or _snprintf is like sprintf

my mistake oops. sorry.

0 new messages