Definition

MITRE’s description of CWE-787: The product writes data past the end, or before the beginning, of the intended buffer.

Common consequences MITRE lists

  • Integrity: Modify Memory; Execute Unauthorized Code or Commands
  • Availability: DoS: Crash, Exit, or Restart
  • Other: Unexpected State

Why it matters

Writing past a buffer corrupts adjacent memory, a common root cause of remote code execution in C and C++ code.

How to prevent it

  • Prefer memory-safe languages such as Rust, Go, Java, C# or Swift for new code, especially for parsers and network-facing components.
  • In C and C++, use bounds-checked containers and APIs, and check every length and index calculation against the destination size, including integer overflow.
  • Compile with hardening such as stack protectors and _FORTIFY_SOURCE, and keep ASLR and non-executable memory enabled.
  • Fuzz parsers and decoders under AddressSanitizer and UndefinedBehaviorSanitizer, and keep each crashing input as a regression test.

Guides on this site

Practice

References

  1. CWE-787 in the MITRE CWE list (version 4.20)
  2. 2025 CWE Top 25 Most Dangerous Software Weaknesses (MITRE CWE Program)

List data from MITRE; the name, definition and consequences are from MITRE’s CWE 4.20 catalogue. A guide is listed as the main subject when the weakness is what the guide is about, and as covering it when a dedicated section or worked example addresses it.

CWE content: Copyright © 2006–2026, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation. CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. Accordingly, The MITRE Corporation hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. CWE is a trademark of The MITRE Corporation. CWE Terms of Use.