---
title: "CWE-785: Use of Path Manipulation Function without Maximum-sized Buffer"
url: https://radicalnotion.ai/cwe/cwe-785
type: cwe
description: "The product invokes a function for normalizing paths or file names, but it provides an output buffer that is smaller than the maximum possible size, such as PATH_MAX."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-785: Use of Path Manipulation Function without Maximum-sized Buffer

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Incomplete
- **CVEs with this as primary weakness:** 0
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## Description

The product invokes a function for normalizing paths or file names, but it provides an output buffer that is smaller than the maximum possible size, such as PATH\_MAX.

Path manipulation functions include realpath(), readlink(), PathAppend(), and others.

## Background

Windows provides a large number of utility functions that manipulate buffers containing filenames. In most cases, the result is returned in a buffer that is passed in as input. (Usually the filename is modified in place.) Most functions require the buffer to be at least MAX\_PATH bytes in length, but you should check the documentation for each function individually. If the buffer is not large enough to store the result of the manipulation, a buffer overflow can occur.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

**Languages:** C (Undetermined), C++ (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity, Confidentiality, Availability | Modify Memory, Execute Unauthorized Code or Commands, DoS: Crash, Exit, or Restart | Passing an inadequately-sized output buffer to a path manipulation function can result in a buffer overflow. |

## Potential mitigations

### Implementation

Always specify output buffers large enough to handle the maximum-size possible result from path manipulation functions.

## Detection methods

### Automated Static Analysis

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

## Demonstrative examples

### Example 1

In this example the function creates a directory named "output\\\<name\>" in the current directory and returns a heap-allocated copy of its name.

Bad code:

```c
char *createOutputDirectory(char *name) {
    char outputDirectoryName[128];
    if (getCurrentDirectory(128, outputDirectoryName) == 0) {
        return null;
    }
    if (!PathAppend(outputDirectoryName, "output")) {
        return null;
    }
    if (!PathAppend(outputDirectoryName, name)) {

        return null;
    }
    if (SHCreateDirectoryEx(NULL, outputDirectoryName, NULL) != ERROR_SUCCESS) {

        return null;
    }
    return StrDup(outputDirectoryName);
}
```

For most values of the current directory and the name parameter, this function will work properly. However, if the name parameter is particularly long, then the second call to PathAppend() could overflow the outputDirectoryName buffer, which is smaller than MAX\_PATH bytes.

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-20](https://radicalnotion.ai/cwe/cwe-20) | Improper Input Validation |
| Parent | [CWE-676](https://radicalnotion.ai/cwe/cwe-676) | Use of Potentially Dangerous Function |
| Parent | [CWE-787](https://radicalnotion.ai/cwe/cwe-787) | Out-of-bounds Write |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| 7 Pernicious Kingdoms | Often Misused: File System | - | - |
| Software Fault Patterns | Faulty String Expansion | SFP9 | - |

## Frequently asked questions

### What is CWE-785?

The product invokes a function for normalizing paths or file names, but it provides an output buffer that is smaller than the maximum possible size, such as PATH\_MAX.

### How do you prevent CWE-785?

Always specify output buffers large enough to handle the maximum-size possible result from path manipulation functions.

### How is CWE-785 detected?

Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-785?

Exploiting CWE-785 can lead to: Modify Memory, Execute Unauthorized Code or Commands, DoS: Crash, Exit, or Restart.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/785.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-785). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
