Definition

MITRE’s description of CWE-122: A heap overflow condition is a buffer overflow, where the buffer that can be overwritten is allocated in the heap portion of memory, generally meaning that the buffer was allocated using a routine such as malloc().

Common consequences MITRE lists

  • Availability: DoS: Crash, Exit, or Restart; DoS: Resource Consumption (CPU); DoS: Resource Consumption (Memory)
  • Integrity, Confidentiality, Availability, Access Control: Execute Unauthorized Code or Commands; Bypass Protection Mechanism; Modify Memory
  • Integrity, Confidentiality, Availability, Access Control, Other: Execute Unauthorized Code or Commands; Bypass Protection Mechanism; Other

Why it matters

Overwriting heap memory corrupts allocator metadata and neighboring objects, which attackers can shape into code execution.

How to prevent it

  • Validate sizes before allocating and copying, including overflow in size calculations.
  • Prefer memory-safe languages and containers that track their capacity.
  • Use a hardened allocator and fuzz with AddressSanitizer.

Guides on this site

Practice

References

  1. CWE-122 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.