0

I was reading about the vulnerabilities in strings in C and then I came across this code. Could anyone give me an explanation why this is vulnerable? Thanks in advance.

g.het
  • 1
  • 2

1 Answers1

1
char buf[MAXSIZE];
if (strlen(str) > MAXSIZE)
    return;
strcpy(buf, str);                

This code incorrectly check for maximum length. C string have a nul at the end of the string to mark the end of the string. strcpy() will copy it. To accomodate for this nul, the test must be:

if (strlen(str) >= MAXSIZE)

The code below is also incorrect. int i[10]; int j = 0;

while (j < 10000){
    i[j] = 5;                
    ++j;
}

This code will overwrite the data in memory after the 10 integers of array i. This will create an undefined behavior, probably a crash but maybe something more subtle...

fpiette
  • 11,983
  • 1
  • 24
  • 46