This is dangerous as sprintf can write more bytes than there is space in the buffer buff. It would be better to use snprintf which does a bounds check. But then one would have to take a look at the return value because this function can fail. In that case one would have to emit an exception.
I can understand that std::ostringstream::operator<< is not as usable as a printf-style format string. Using Boost format would be a viable alternative: easy and safe to use.
There is an idiom often used in the code:
This is dangerous as
sprintf
can write more bytes than there is space in the bufferbuff
. It would be better to usesnprintf
which does a bounds check. But then one would have to take a look at the return value because this function can fail. In that case one would have to emit an exception.I can understand that
std::ostringstream::operator<<
is not as usable as aprintf
-style format string. Using Boost format would be a viable alternative: easy and safe to use.