CWE-787: Out-of-bounds write | Causes and Fixes Skip to content

Security weaknessCWE Top 25 · #5

CWE-787 Out-of-bounds write

Software writes data outside the memory it reserved, which can crash it or let attackers run their own code.

  • Senior security engineers
  • 540+ projects delivered
  • Reply within one business day

Quick answer

What is CWE-787?

Out-of-bounds Write: The product writes data past the end, or before the beginning, of the intended buffer.

Business impact

What CWE-787 could cost your business

MITRE documents what this weakness can do. Here is what that means for a website, store or app.

Integrity

Data, pages or code changed

Prices, orders, content or the code your site runs can be altered without your approval.

  • Modify Memory
  • Execute Unauthorized Code or Commands

Availability

Downtime and lost sales

Crashes, slowdowns or exhausted resources can take your site, store or app offline.

  • DoS: Crash, Exit, or Restart

In plain English

What Out-of-bounds write means for your business

  • 540+projects delivered
  • 9+years in business
  • 23+certified experts
Get a free security check

A senior engineer replies within one business day.

Programs store data in blocks of memory called buffers, each with a fixed size. An out-of-bounds write happens when code puts data past the end of a buffer, or before its start. The extra data lands on whatever sits next to it in memory. That neighbor might be other data, or it might be information the program uses to decide what to run next. This mostly affects languages like C and C++ that do not check sizes for you.

You are unlikely to write this bug yourself in PHP, JavaScript or Python. You are exposed through the software underneath: the web server, the language runtime, image and document libraries, native mobile code and device firmware. When one of those has this flaw, a crafted file or request can crash the service or, in the worst case, let an attacker run code on the machine. That is why patching the stack your site depends on matters.

Where it shows up

  • Image, video, font and PDF processing libraries written in C or C++ that your site calls when a customer uploads a file or a thumbnail is generated.
  • Native mobile apps for iOS and Android that include C or C++ code for media, compression, cryptography or game engines.
  • C or C++ extensions for PHP, Python or Node.js, which run native code inside an otherwise memory-safe application.
  • Embedded and IoT firmware in routers, cameras, payment terminals and smart devices, plus outdated web server or database software on your hosting.

Free security check

Is CWE-787 hiding in your site?

A senior engineer checks your site, store or app for this weakness and the rest of the CWE Top 25, then sends a prioritized fix plan within one business day. No obligation.

Official definition

How MITRE defines CWE-787

Out-of-bounds Write

The product writes data past the end, or before the beginning, of the intended buffer.

Also known as: Memory Corruption

Weakness ID
CWE-787
Type
Base
Status
Draft
Likelihood of exploit
High
Applies to
Memory-Unsafe, C, C++, Assembly, ICS/OT
Introduced during
Implementation
2025 CWE Top 25
Rank 5

Fix it

How to fix Out-of-bounds write

Practical steps in priority order. Share them with your developer or agency.

  1. Patch the software you depend on

    Keep the operating system, web server, language runtime and native libraries on supported versions and apply security updates promptly. Track what you run with a software inventory, and subscribe to vendor security advisories.

  2. Prefer memory-safe languages

    Write new components in a language that checks bounds automatically, such as Rust, Go, Java, C# or Swift. Where C or C++ must stay, keep the native part small and wrap it behind a narrow, well-tested interface.

  3. Check sizes before every write

    In native code, validate lengths and indexes against the buffer size before copying or writing. Replace unbounded functions such as strcpy and sprintf with length-aware versions, and watch for integer overflow in size calculations.

  4. Enable compiler and OS protections

    Build with stack protectors, fortified source checks and control-flow protections. Keep address space layout randomization and non-executable memory enabled. These do not remove the bug, but they make it much harder to turn into code execution.

  5. Fuzz and sanitize in testing

    Run fuzz testing, which feeds large volumes of malformed input to a program, against parsers and file handlers. Use AddressSanitizer in test builds and static analysis in continuous integration to catch out-of-bounds writes before release.

Signs you may be affected

  • Your server runs operating system packages, runtimes or libraries that are past end of support.
  • A service crashes or restarts when it processes certain uploaded files or unusual requests.
  • Dependency or container scans report memory corruption advisories in native components you ship.

Questions to ask your developer

  • Which parts of our stack are written in C or C++?
  • How quickly do we apply security updates to the server and its libraries?
  • Do we process uploaded files in an isolated, low-privilege process?

Prevention

What MITRE recommends, phase by phase

Every mitigation from the CWE entry, in the order a project runs: plan, build, test, operate.

  1. 01

    Requirements

    • Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, many languages that perform their own memory management, such as Java and Perl, are not subject to buffer overflows. Other languages, such as Ada and C#, typically provide overflow protection, but the protection can be disabled by the.
  2. 02

    Architecture and Design

    • Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. Examples include the Safe C String Library (SafeStr) by Messier and Viega [REF-57], and the Strsafe.h library from Microsoft [REF-56]. These libraries provide safer versions of overflow-prone string-handling functions.
  3. 03

    Implementation

    • Consider adhering to the following rules when allocating and managing an application’s memory: Double check that the buffer is as large as specified. When using functions that accept a number of bytes to copy, such as strncpy(), be aware that if the destination buffer size is equal to the source buffer size, it may not NULL-terminate the string. Check buffer boundaries if.
    • Replace unbounded copy functions with analogous functions that support length arguments, such as strcpy with strncpy. Create these if they are not available.
  4. 04

    Build and Compilation

    • Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include: the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice, which provide various mechanisms including canary-based detection and range/index checking. D3-SFCV (Stack Frame Canary Validation) from D3FEND.
    • Run or compile the software using features or extensions that randomly arrange the positions of a program’s executable and libraries in memory. Because this makes the addresses unpredictable, it can prevent an attacker from reliably jumping to exploitable code. Examples include Address Space Layout Randomization (ASLR) [REF-58] [REF-60] and Position-Independent Executables.
  5. 05

    Operation

    • Use a CPU and operating system that offers Data Execution Protection (using hardware NX or XD bits) or the equivalent techniques that simulate this feature in software, such as PaX [REF-60] [REF-61]. These techniques ensure that any instruction executed is exclusively at a memory address that is part of the code segment. For more information on these techniques see D3-PSEP.

Detection and evidence

How CWE-787 is found, and where it has hit

The testing methods MITRE recommends, and real vulnerabilities this weakness has caused.

How it is detected

  • Automated Static AnalysisThis weakness can often be detected using automated static analysis tools. Many modern tools use data flow analysis or constraint-based techniques to minimize the number of false positives. Automated static analysis generally does not account for environmental considerations when reporting.
  • Automated Dynamic AnalysisThis weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The software’s operation may slow down, but it should not become unstable.
  • Automated Dynamic AnalysisUse tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ [REF-1518].

Real vulnerabilities it caused

  • CVE-2026-7270Chain: calculation expression does not use parentheses to address operator precedence (CWE-783), causing an incorrect calculation (CWE-682) leading to a buffer overflow (CWE-787).
  • CVE-2025-27363Font rendering library does not properly handle assigning a signed short value to an unsigned long (CWE-195), leading to an integer wraparound (CWE-190), causing too small of a buffer (CWE-131), leading to an.
  • CVE-2023-1017The reference implementation code for a Trusted Platform Module does not implement length checks on data, allowing for an attacker to write 2 bytes past the end of a buffer.
  • CVE-2021-21220Chain: insufficient input validation (CWE-20) in browser allows heap corruption (CWE-787), as exploited in the wild per CISA KEV.
  • CVE-2021-28664GPU kernel driver allows memory corruption because a user can obtain read/write access to read-only pages, as exploited in the wild per CISA KEV.
  • CVE-2020-17087Chain: integer truncation (CWE-197) causes small buffer allocation (CWE-131) leading to out-of-bounds write (CWE-787) in kernel pool, as exploited in the wild per CISA KEV.

How we fix it

From finding to fixed: how Codefreex closes CWE-787

  1. Scan and confirm

    We test your site, store or app for this weakness and the rest of the CWE Top 25 with automated scanning and manual review, and confirm what is actually exploitable.

  2. Fix at the source

    We change the code, configuration or dependency that causes it, not just the symptom, and plan the changes to avoid downtime.

  3. Verify the fix

    We retest every finding, check that nothing else broke and hand you a short report you can share with your team, client or auditor.

  4. Keep it fixed

    Updates, monitoring and maintenance so the weakness does not return with the next plugin, theme, app or code change.

FAQ

CWE-787: FAQs

Short answers for owners and developers. Not sure whether your site is affected? Ask us to check.

Should a website owner worry about out-of-bounds writes?

Yes, but indirectly. Your own PHP or JavaScript code is very unlikely to contain one. The risk sits in the native software your site runs on, such as image libraries, the web server and the language runtime. If those are outdated, a crafted upload or request may reach a known flaw. Keeping them patched is your main defense.

How severe is an out-of-bounds write?

It is among the most severe classes of weakness. At minimum it can crash the program, causing downtime. In worse cases an attacker can overwrite data that controls what the program runs next and execute their own code. MITRE rates the likelihood of exploitation as high. The real impact depends on the affected component and the protections enabled.

How are out-of-bounds writes prevented?

Use memory-safe languages where possible. In C and C++, check every length and index, avoid unbounded copy functions and use safer library types. Compile with modern protections turned on. Test parsers with fuzzing and memory sanitizers. For software you do not write, stay on supported versions and install security updates as they are released.

What is CWE-787?

CWE-787, “Out-of-bounds Write”, is a software weakness catalogued by MITRE in the Common Weakness Enumeration. The product writes data past the end, or before the beginning, of the intended buffer.

How serious is CWE-787?

MITRE rates the likelihood of exploit as High. It can affect integrity, availability, other, with impacts such as modify memory, execute unauthorized code or commands, dos: crash, exit, or restart. It ranks number 5 on the 2025 CWE Top 25 Most Dangerous Software Weaknesses. The real risk depends on where the weakness sits and what data or functions it exposes.

How do you prevent CWE-787?

Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, many languages that perform their own memory management, such as Java and Perl, are not subject to buffer overflows. Other. Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. Examples include the Safe C String Library (SafeStr) by.

Can Codefreex check my site for CWE-787?

Yes. A senior engineer reviews your site, store or app for CWE-787 and the rest of the CWE Top 25, confirms what is exploitable and sends a prioritized fix plan within one business day. Start with the free security check form on this page.

Free security check

Find out if CWE-787 affects your site

Send your URL. A senior engineer checks for this weakness and the rest of the CWE Top 25, and replies within one business day with what to fix first.

  • A senior strategist reviews your request, not a sales bot
  • Clear scope, timeline and estimate within one business day
  • Free 30-minute discovery call, no obligation
  • NDA available on request

Weakness data from the Common Weakness Enumeration (CWE®) version 5.0, Copyright © 2006–2026, The MITRE Corporation. CWE is free to use by any organization or individual for any research, development, and/or commercial purposes under the CWE Terms of Use. CWE is a registered trademark of The MITRE Corporation. The business impact, risk profile and plain-English guidance on this page are written by Codefreex and are general information, not a security assessment of your system.