CWE-785¶
Use of Path Manipulation Function without Maximum-sized Buffer. [Improper-Adherence-To-Coding-Standards]
Required inputs: IR
Background Details Demonstrative Examples
Background Details 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.
Example Language: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.
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See 9.4 3rd-Party Licenses in the documentation for full details.Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
undersized_buffer |
Undersized buffer passed to path manipulation function. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
fallback_max_path_size¶
fallback_max_path_size : int | None = None
functions¶
functions
Path manipulation functions to check, mapped to the index of their output-buffer argument.Type: dict[str, int]
Default:
{ 'PathAppend': 0, 'PathAppendA': 0, 'PathAppendW': 0, 'readlink': 1, 'realpath': 1 }
max_size_macros¶
max_size_macros : list[str] = ['PATH_MAX', 'MAX_PATH']