Search code examples
cstringpointersklocwork

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


Solution

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