One problem with ANSI C is the lack of a safe version of
sprintf(), adressed by C99 with snprintf(). I need it because:
- we want some rich error/debug messages, including the file and line number, and...
- file and line number are provided by the ANSI C
__FILE__and__LINE__macros, so... - we must call the messages functions with a macro, but ...
- we sometimes need to send printf-like formatted messages, like
("the value is %i", foo), and ... - ANSI C doesn't allow variadic macros, so ...
- we must use an intermediary function to format the message and store
it in a temporary buffer, like
DEBUG(vmsg("the value is %i", foo)), but ... - the temporary buffer has a fixed length, and ...
sprintf()may write after the end of this buffer, which would result in a crash.
So, as a workaround, we use an "ANSI C implementation of snprintf()". Various implementations exist, such as:
For the moment, we use the latter one, because it's more compact.
Bad news : none of these implementations are free of splint or gcc
warnings... so they have to be excluded from our code quality tests.