CWE Rule 909
Description
The product does not initialize a critical resource
Polyspace Implementation
Polyspace® checks for these issues
Use of standard library string routine on noninitialized buffer
Non initialized pointer
Examples
This issue occurs when a string library function is called with a buffer that is not initialized.
When string operations are performed on a noninitialized buffer, the resulting string is unpredictable and can result in unexpected behavior.
Initialize string buffers before performing string operations on them.
In this example, the buffer buff is not initialized before it is
used as input to strcat and printf. Because the
buffer is not initialized, it may be filler with junk value. When "foo"
is concatenated to it, the result is unpredictable.
#include <stdio.h>
#include <string.h>
void example_NonCompliant() {
char buff[20];
strcat(buff, "foo"); //Noncompliant
printf("%s\n", buff);
}
int main(void) {
example_NonCompliant();
return 0;
}The corrected version initializes the buffer before using it in library functions.
#include <stdio.h>
#include <string.h>
void example_NonCompliant() {
char buff[20] = "";
strcat(buff, "foo"); //Compliant
printf("%s\n", buff);
}
int main(void) {
example_NonCompliant();
return 0;
}This issue occurs when a pointer is not assigned an address before dereference.
Unless a pointer is explicitly assigned an address, it points to an unpredictable location.
The fix depends on the root cause of the defect. For instance, you assigned an address to the pointer but the assignment is unreachable.
Often the result details (or source code tooltips in Polyspace as You Code™) show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show this event history, you can search for previous references of variables relevant to the defect using right-click options in the source code and find related events. See also Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
See examples of fixes below. It is a good practice to initialize a pointer to NULL when declaring the pointer.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Polyspace Results 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 <stdlib.h>
int* assign_pointer(int* prev)
{
int j = 42;
int* pi;
if (prev == NULL)
{
pi = (int*)malloc(sizeof(int));
if (pi == NULL) return NULL;
}
*pi = j;
/* Defect: Writing to uninitialized pointer */
return pi;
}If prev is not NULL, the
pointer pi is not assigned an address. However, pi
is dereferenced on every execution paths, irrespective of whether
prev is NULL or not.
One possible correction is to assign an address to
pi when prev is not
NULL.
#include <stdlib.h>
int* assign_pointer(int* prev)
{
int j = 42;
int* pi;
if (prev == NULL)
{
pi = (int*)malloc(sizeof(int));
if (pi == NULL) return NULL;
}
/* Fix: Initialize pi in branches of if statement */
else
pi = prev;
*pi = j;
return pi;
}Check Information
| Category: Resource Management Errors |
PQL Name:
std.cwe_native.R909
|
Version History
Introduced in R2026a
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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)