LWN.net Logo

snprintf() confusion

snprintf() confusion

Posted Feb 5, 2004 16:33 UTC (Thu) by hazelsct (subscriber, #3659)
In reply to: snprintf() confusion by ThePythonicCow
Parent article: snprintf() confusion

Hmm, seems like a better approach might be to track down such uses of snprintf() and replace them with something like:

if ((len += snprintf (buf+len, buflen-len, "...", ...)) > buflen) {
        optionally deal with the error;
        len = buflen;
}
The branch shouldn't cost more than the call to snprintf (unless it's inlined and extremely efficient), and you get to deal with error conditions, making it better than the old snprintf behavior.

Of course, this takes time (janitors?), but aside from that, I'm not sure I see why this snprintf behavior is a problem...


(Log in to post comments)

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds