Out-of-bounds Write High

This is a type of memory access error that occurs when a program writes data from a memory address outside of the bounds of a buffer. This can result in the program writing data that does not belong to it, which can cause crashes, incorrect behavior, or even security vulnerabilities.

Detector ID
c/out-of-bounds-write@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1void outOfBoundsWriteNonCompliant(){
2    // Declaring an array named id_sequence with a size of 3 integers
3    int id_sequence[3];
4    id_sequence[0] = 123;
5    id_sequence[1] = 234; 
6    id_sequence[2] = 345;
7    // Noncompliant: Attempting to assign a value to the fourth element (out of bounds)
8    id_sequence[3] = 456; 
9}

Compliant example

1#include <stdio.h>
2
3void outOfBoundsWriteCompliant(){
4
5  // Compliant: Ensuring correct loop bounds
6  int arr[3] = {1, 2, 3};
7  for (int i = 0; i < 3; ++i) {
8    arr[i] = i * 2; // Accessing indices within array bounds 
9  }
10}