An integer overflow might occur when the input or resulting value is too large to store in associated representation. This can result in a critical security issue when it is used to make security decisions.
1#include <stdlib.h>
2
3void integerOverflowNoncompliant(int *ptr, size_t offset) {
4 // Noncompliant: Perform pointer arithmetic without checking for potential integer overflow.
5 int *result = ptr + offset;
6}
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdint.h>
4
5void integerOverflowCompliant(int *ptr, size_t offset) {
6 // Compliant: Safer pointer arithmetic with proper check
7 if (offset <= SIZE_MAX / sizeof(int))
8 {
9 int *result = ptr + offset;
10 // Use 'result'
11 }
12 else
13 {
14 fprintf(stderr, "Overflow detected in pointer arithmetic.\n");
15 }
16}