今日工程中用sscanf时候出现 *** stack smashing detected ***
的问题。
问题
#include <stdio.h>
void test()
{
unsigned short buf[6] = {
0};for(int i=0;i <6;i++)printf("before sscanf buf[%d] = %d\n", i, buf[i]);sscanf("2021-06-07 17:55:05", "%d-%d-%d %d:%d:%d", &buf[0], &buf[1],&buf[2], &buf[3], &buf[4], &buf[5]);for(int i=0 ;i<6; i++)printf("after sscanf buf[%d] = %d\n", i, buf[i]);
}
int main()
{
test();return 0;
}
输出结果:
root@ubuntu:~/Desktop/test# ./testCode
before sscanf buf[0] = 0
before sscanf buf[1] = 0
before sscanf buf[2] = 0
before sscanf buf[3] = 0
before sscanf buf[4] = 0
before sscanf buf[5] = 0
after sscanf buf[0] = 2021
after sscanf buf[1] = 6
after sscanf buf[2] = 7
after sscanf buf[3] = 17
after sscanf buf[4] = 55
after sscanf buf[5] = 5
*** stack smashing detected ***: terminated
已放弃 (核心已转储)
问题解决:
将 unsigned short buf[6] = {0};
修改为 unsigned int buf[6] = {0};