sprintf_s with a buffer too small

后端 未结 6 1219
广开言路
广开言路 2021-02-08 12:03

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).



        
6条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 12:24

    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.

提交回复
热议问题