Hi all, There are a bunch of sprintf calls in the FLAC code base and I'd like to replace them with calls to snprintf but I know that can cause problems with at least some versions of Visual Studio. Whats the current state of play in this regard? Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
On 3/14/2013 8:00 AM, Erik de Castro Lopo wrote:> Hi all, > > There are a bunch of sprintf calls in the FLAC code base and I'd like > to replace them with calls to snprintf but I know that can cause > problems with at least some versions of Visual Studio. > > Whats the current state of play in this regard? > > ErikSince MS refuses to support C99, the common practice is to use either sprintf_s: http://msdn.microsoft.com/en-us/library/ce3zzk1k(v=VS.80).aspx _snprintf_s: http://msdn.microsoft.com/de-de/library/f30dzcf6(v=VS.80).aspx The former can be used as a drop-in replacement of snprintf via a define, the latter takes an additional argument count which can limit the bytes written to the buffer additionally. DO NOT use _snprintf: http://msdn.microsoft.com/de-de/library/2ts7cx93(v=vs.80).aspx since this does not NULL terminate if the length of the buffer is equal to the given buffer size (or larger than), and this differs from the std behaviour as stated at e.g. http://linux.die.net/man/3/snprintf Christoph
Christoph Terasa wrote:> Since MS refuses to support C99, the common practice is to use either > > sprintf_s: > http://msdn.microsoft.com/en-us/library/ce3zzk1k(v=VS.80).aspx > _snprintf_s: > http://msdn.microsoft.com/de-de/library/f30dzcf6(v=VS.80).aspx > > The former can be used as a drop-in replacement of snprintf via a > define, the latter takes an additional argument count which can limit > the bytes written to the buffer additionally.Hmm, the first can be used as a drop-in replacement for many but not all uses of ISO C99 snprintf. For the uses in FLAC, it should be sufficient, we just have to be careful not to rely on the return value.> DO NOT use > _snprintf: > http://msdn.microsoft.com/de-de/library/2ts7cx93(v=vs.80).aspx > > since this does not NULL terminate if the length of the buffer is equal > to the given buffer size (or larger than), and this differs from the std > behaviour as stated at e.g. http://linux.die.net/man/3/snprintfYes, now I remember, that one is horribly broken. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
Erik de Castro Lopo wrote:> Hi all, > > There are a bunch of sprintf calls in the FLAC code base and I'd like > to replace them with calls to snprintf but I know that can cause > problems with at least some versions of Visual Studio. > > Whats the current state of play in this regard?Does MSVS have <stdarg.h>? Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
Erik de Castro Lopo wrote:> Does MSVS have <stdarg.h>?Hmm, seems to: http://msdn.microsoft.com/en-us/library/kb57fad8%28v=vs.80%29.aspx Hope it works as expected. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/