CWE Rule 785
Description
Rule Description
The software 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.
Polyspace Implementation
The rule checker checks for Use of path manipulation function without maximum-sized buffer checking.
Examples
Use of path manipulation function without maximum-sized buffer checking
This issue occurs when the destination argument of a path manipulation function such
as realpath
or getwd
has a buffer size less than
PATH_MAX
bytes.
A buffer smaller than PATH_MAX
bytes can overflow but you
cannot test the function return value to determine if an overflow occurred. If an
overflow occurs, following the function call, the content of the buffer is
undefined.
For instance, char *getwd(char *buf)
copies an absolute path
name of the current folder to its argument. If the length of the absolute path name
is greater than PATH_MAX
bytes, getwd
returns
NULL
and the content of *buf
is undefined.
You can test the return value of getwd
for
NULL
to see if the function call succeeded.
However, if the allowed buffer for buf
is less than
PATH_MAX
bytes, a failure can occur for a smaller absolute
path name. In this case, getwd
does not return
NULL
even though a failure occurred. Therefore, the allowed
buffer for buf
must be PATH_MAX
bytes
long.
Possible fixes are:
Use a buffer size of
PATH_MAX
bytes. If you obtain the buffer from an unknown source, before using the buffer as argument ofgetwd
orrealpath
function, make sure that the size is less thanPATH_MAX
bytes.Use a path manipulation function that allows you to specify a buffer size.
For instance, if you are using
getwd
to get the absolute path name of the current folder, usechar *getcwd(char *buf, size_t size);
instead. The additional argumentsize
allows you to specify a size greater than or equal toPATH_MAX
.Allow the function to allocate additional memory dynamically, if possible.
For instance,
char *realpath(const char *path, char *resolved_path);
dynamically allocates memory ifresolved_path
isNULL
. However, you have to deallocate this memory later using thefree
function.
#include <unistd.h> #include <linux/limits.h> #include <stdio.h> void func(void) { char buf[PATH_MAX]; if (getwd(buf+1)!= NULL) //Noncompliant { printf("cwd is %s\n", buf); } }
In this example, although the array buf
has PATH_MAX
bytes,
the argument of getwd
is buf + 1
,
whose allowed buffer is less than PATH_MAX
bytes.
PATH_MAX
BytesOne possible correction is to use an array argument with size
equal to PATH_MAX
bytes.
#include <unistd.h> #include <linux/limits.h> #include <stdio.h> void func(void) { char buf[PATH_MAX]; if (getwd(buf)!= NULL) { printf("cwd is %s\n", buf); } }
Check Information
Category: Others |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)