how string are represented in memory in c
题
char imei_temp[14] = {0, };
strcpy(imei_temp, "00000000000000");
According to my understanding this is valid code.
But Klocwork is saying Buffer overflow, array index of 'imei_temp
' may be out of bounds. Array 'imei_temp
' of size 14 may use index value(s) 0..14
解决方案
It's a buffer overflow because your buffer is 14 bytes, but you are writing 15 bytes to it: 14 ascii "0"'s, and a null byte at the end.
其他提示
when you specify a string using "
s it adds an implicit \0
to the end of the string, you're trying to copy 15 bytes in to a 14 byte buffer.
Note, this doesn't happen when you specify a character using '
s.
不隶属于 StackOverflow