CWE Rule 135
Description
Rule Description
The software does not correctly calculate the length of strings that can contain wide or multi-byte characters.
Polyspace Implementation
The rule checker checks for these issues:
Destination buffer overflow in string manipulation
Misuse of narrow or wide character string
Unreliable cast of pointer
Examples
Destination buffer overflow in string manipulation
This issue occurs when certain string manipulation functions write to their destination buffer argument at an offset greater than the buffer size.
For instance, when calling the function sprintf(char*
buffer, const char* format)
, you use a constant string format
of
greater size than buffer
.
Buffer overflow can cause unexpected behavior such as memory corruption or stopping your system. Buffer overflow also introduces the risk of code injection.
One possible solution is to use alternative functions to constrain the number of characters written. For instance:
If you use
sprintf
to write formatted data to a string, usesnprintf
,_snprintf
orsprintf_s
instead to enforce length control. Alternatively, useasprintf
to automatically allocate the memory required for the destination buffer.If you use
vsprintf
to write formatted data from a variable argument list to a string, usevsnprintf
orvsprintf_s
instead to enforce length control.If you use
wcscpy
to copy a wide string, usewcsncpy
,wcslcpy
, orwcscpy_s
instead to enforce length control.
Another possible solution is to increase the buffer size.
#include <stdio.h> void func(void) { char buffer[20]; char *fmt_string = "This is a very long string, it does not fit in the buffer"; sprintf(buffer, fmt_string); //Noncompliant }
In this example, buffer
can contain 20 char
elements
but fmt_string
has a greater size.
snprintf
Instead
of sprintf
One possible correction is to use the snprintf
function
to enforce length control.
#include <stdio.h> void func(void) { char buffer[20]; char *fmt_string = "This is a very long string, it does not fit in the buffer"; snprintf(buffer, 20, fmt_string); }
Misuse of narrow or wide character string
This issue occurs when you pass a narrow character string to a wide string function, or a wide character string to a narrow string function.
Misuse of narrow or wide character string raises no defect on operating systems where narrow and wide character strings have the same size.
Using a narrow character string with a wide string function, or vice versa, can result in unexpected or undefined behavior.
If you pass a wide character string to a narrow string function, you can encounter these issues:
Data truncation. If the string contains null bytes, a copy operation using
strncpy()
can terminate early.Incorrect string length.
strlen()
returns the number of characters of a string up to the first null byte. A wide string can have additional characters after its first null byte.
If you pass a narrow character string to a wide string function, you can encounter this issue:
Buffer overflow. In a copy operation using
wcsncpy()
, the destination string might have insufficient memory to store the result of the copy.
Use the narrow string functions with narrow character strings. Use the wide string functions with wide character strings.
#include <string.h> #include <wchar.h> void func(void) { wchar_t wide_str1[] = L"0123456789"; wchar_t wide_str2[] = L"0000000000"; strncpy(wide_str2, wide_str1, 10); //Noncompliant }
In this example, strncpy()
copies 10 wide characters from
wide_strt1
to wide_str2
. If
wide_str1
contains null bytes, the copy operation can end prematurely
and truncate the wide character string.
wcsncpy()
to Copy Wide Character StringsOne possible correction is to use wcsncpy()
to copy
wide_str1
to wide_str2
.
#include <string.h> #include <wchar.h> void func(void) { wchar_t wide_str1[] = L"0123456789"; wchar_t wide_str2[] = L"0000000000"; wcsncpy(wide_str2, wide_str1, 10); }
Unreliable cast of pointer
This issue occurs
when a pointer is implicitly cast to a data type different from its
declaration type. Such an implicit casting can take place, for instance,
when a pointer to data type char
is assigned the
address of an integer.
This defect applies only if the code language for the project is C.
Casting a pointer to data type different from its declaration type can result in issues such as buffer overflow. If the cast is implicit, it can indicate a coding error.
Avoid implicit cast of a pointer to a data type different from its declaration type.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <string.h> void Copy_Integer_To_String() { int src[]={1,2,3,4,5,6,7,8,9,10}; char buffer[]="Buffer_Text"; strcpy(buffer,src); //Noncompliant /* Defect: Implicit cast of (int*) to (char*) */ }
src
is declared as an int*
pointer. The strcpy
statement, while copying to buffer
, implicitly casts src
to char*
. In C++, such a cast fails to compile.
One possible correction is to declare the pointer src
with
the same data type as buffer
.
#include <string.h> void Copy_Integer_To_String() { /* Fix: Declare src with same type as buffer */ char *src[10]={"1","2","3","4","5","6","7","8","9","10"}; char *buffer[10]; for(int i=0;i<10;i++) buffer[i]="Buffer_Text"; for(int i=0;i<10;i++) buffer[i]= src[i]; }
Check Information
Category: String Errors |
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 (한국어)