这个见过的题好像比较多
应该这样用
int sprintf(char *restrict str,
const char *restrict format, ...);
int snprintf(char str[restrict .size], size_t size,
const char *restrict format, ...);
先是format,然后再是parse argv
错误举例:
snprintf(buf, "%s", input, 0x10)
snprintf的返回值并不是返回parse了多少个bytes
而是返回能够parse多少bytes
也就是
#include <stdio.h>
#include <string.h>
int main (){
char buf[0x30];
memset(buf, 0, 0x30);
char a[0x60];
memset(a, 'a', 0x60);
int ret = snprintf(buf, 0x30, "%s", a);
printf("Written counts: %d\\n", ret);
return 0;
}
这个例子返回值为0x60,超过了snprintf的limit 0x30
如果a为受控输入且snprintf返回值在后续被使用,可能导致溢出等问题
当全局变量被当作局部变量使用时,如果没有对全局变量进行重置/初始化,会出现一些逻辑问题
浮点数Nan在进行大于小于比较的时候总会返回false