CWE Rule 463
Description
Rule Description
The accidental deletion of a data-structure sentinel can cause serious programming logic problems.
Polyspace Implementation
The rule checker checks for Missing null in string array.
Examples
Missing null in string array
This issue occurs when a string does not have enough
space to terminate with a null character '\0'
.
This defect applies only for projects in C.
A buffer overflow can occur if you copy a string to an array without assuming the implicit null terminator.
If you initialize a character array with a literal, avoid specifying the array bounds.
char three[] = "THREE";
If the issue occurs after initialization, you might have to increase the size of the array by one to account for the null terminator.
In certain circumstances, you might want to initialize the character array with a sequence of characters instead of a string. In this situation, 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.
void countdown(int i) { static char one[5] = "ONE"; static char two[5] = "TWO"; static char three[5] = "THREE"; //Noncompliant }
The character array three
has a size of 5 and 5 characters 'T'
, 'H'
, 'R'
, 'E'
, and 'E'
. There is no room for the null character at the end because three
is only five bytes large. This issue can cause a compilation error in C++.
One possible correction is to change the array size to allow for the five characters plus a null character.
void countdown(int i) { static char one[5] = "ONE"; static char two[5] = "TWO"; static char three[6] = "THREE"; }
One possible correction is to initialize the string by leaving the array size blank. This initialization method allocates enough memory for the five characters and a terminating-null character.
void countdown(int i) { static char one[5] = "ONE"; static char two[5] = "TWO"; static char three[] = "THREE"; }
Check Information
Category: Data Neutralization Issues |
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 (한국어)