The following code causes an error and kills my application. It makes sense as the buffer is only 10 bytes long and the text is 22 bytes long (buffer overflow).
It's by design. The entire point of sprintf_s
, and other functions from the *_s
family, is to catch buffer overrun errors and treat them as precondition violations. This means that they're not really meant to be recoverable. This is designed to catch errors only - you shouldn't ever call sprintf_s
if you know the string can be too large for a destination buffer. In that case, use strlen
first to check and decide whether you need to trim.