5

I am reading the OWASP page on buffer overflow. It mentions:

buffer overflow can be prevented using higher-level programming languages that are strongly typed and

developer should validate input to prevent unexpected data from being processed, such as of the wrong data type

Can someone give an example when an incorrect data type can lead to a buffer overflow ?

Jake
  • 1,095
  • 3
  • 12
  • 20

1 Answers1

4

Here are some C/C++ examples:

  • Reading a long into an int memory space (say with scanf).

    int i;
    scanf("%ld", &i);
    
  • Using a float as an index in a for loop instead of a fixed point structure, resulting in possibly one more or less iteration than expected.

    char *arr = new char[9];
    for(float j=1; j>0; j=j-0.1) { // will clobber 10 characters, not 9
        *arr++ = 'A';
    
  • copying between arrays with different type widths

    float floatArr[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
    char charArr[10];
    memcpy(charArr, floatArr, sizeof(float));
    
Ari Trachtenberg
  • 842
  • 7
  • 14