snprintf 引发的问题
snprintf 是 C/C++ 程序中最常见的函数了,但是snprintf 在Windows和 Linux上的行为并不一致,对于跨平台的开发,有时会带来严重的缓冲区溢出问题。
本文提供了一个安全检查的版本,保证了 Windows和 Linux上的行为的一致性,任何情况下不会溢出。
建议用户使用 snprintf_chkd_exit 版本,任何错误导致退出程序 ! 这样避免对 snprintf_chkd 的返回值进行检查,保证了逻辑清晰和程序健壮。
/**
* snprintf_chkd()
* A checked version of snprintf() for both GCC and MSVC
*
* see:
* <stdarg.h>
* https://linux.die.net/man/3/snprintf
*
* The functions snprintf() and vsnprintf() do not write more than size bytes
* (including the terminating null byte ('\0')).
* If the output was truncated due to this limit then the return value is the
* number of characters (excluding the terminating null byte) which would have
* been written to the final string if enough space had been available.
* Thus, a return value of size or more means that the output was truncat