> 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/buffer-overflow.md).

# Buffer Overflow

**Example 1: C**

**Vulnerable Code:**

```c
cCopy codechar buffer[10];
strcpy(buffer, "This string is too long");
```

**Reason for vulnerability:** Copying a string that exceeds the buffer size, leading to buffer overflow.

**Fixed Code:**

```c
cCopy codechar buffer[10];
strncpy(buffer, "This string", sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';
```

**Reason for fix:** Use `strncpy` and ensure the buffer is null-terminated.
