> For the complete documentation index, see [llms.txt](https://playbook.sidthoviti.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://playbook.sidthoviti.com/devsecops/secure-coding/code-review-examples/integer-overflow.md).

# Integer Overflow

**Example 1: C**

**Vulnerable Code:**

```c
cCopy codeint a = INT_MAX;
int b = a + 1;
```

**Reason for vulnerability:** Adding 1 to the maximum integer value, causing overflow.

**Fixed Code:**

```c
cCopy codeint a = INT_MAX;
if (a + 1 < a) {
    printf("Integer overflow detected\n");
} else {
    int b = a + 1;
}
```

**Reason for fix:** Check for overflow before performing the addition.

**Example 2: Java**

**Vulnerable Code:**

```java
javaCopy codeint a = Integer.MAX_VALUE;
int b = a + 1;
```

**Reason for vulnerability:** Adding 1 to the maximum integer value, causing overflow.

**Fixed Code:**

```java
javaCopy codeint a = Integer.MAX_VALUE;
if (a + 1 < a) {
    System.out.println("Integer overflow detected");
} else {
    int b = a + 1;
}
```

**Reason for fix:** Check for overflow before performing the addition.
