deprecating sprintf(9)

Poul-Henning Kamp phk at phk.freebsd.dk
Wed Sep 8 16:39:08 UTC 2010


In message <AANLkTikO1v7YMFKVZkHZDmurcyfq0QbTkPxG=LNBdKSp at mail.gmail.com>, mdf@
FreeBSD.org writes:

>It seems like a large project, but OTOH sprintf(9) is mighty unsafe in
>the kernel.

Well, it is only unsafe if people used it without knowing what they
are doing, so I think a wholesale automated replacement is both
unwarranted and inadvisable.

I can recommend the following macro for the static buffer cases, it
checks if people know what they are doing with an assert.

#define bprintf(buf, fmt, ...)                                          \
        do {                                                            \
                assert(snprintf(buf, sizeof buf, fmt, __VA_ARGS__)      \
                    < sizeof buf);                                      \
        } while (0)

Poul-Henning

-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.


More information about the freebsd-current mailing list