Out of Bounds

Out of Bounds

Example 1: C

Vulnerable Code:

cCopy codeint arr[10];
for (int i = 0; i <= 10; i++) {
    arr[i] = i;
}

Reason for vulnerability: Array index goes out of bounds, leading to undefined behavior.

Fixed Code:

cCopy codeint arr[10];
for (int i = 0; i < 10; i++) {
    arr[i] = i;
}

Reason for fix: Ensure the array index is within bounds.

Example 2: Python

Vulnerable Code:

pythonCopy codearr = [0] * 10
for i in range(11):
    arr[i] = i

Reason for vulnerability: Array index goes out of bounds, leading to an IndexError.

Fixed Code:

pythonCopy codearr = [0] * 10
for i in range(10):
    arr[i] = i

Reason for fix: Ensure the array index is within bounds.

Last updated