CWE-665¶
Improper Initialization. [Improper-Control-Of-A-Resource-Through-Its-Lifetime]
Required inputs: IR, StaticSemanticAnalysis
Demonstrative Examples
Example 1
Here, a boolean initialized field is consulted to ensure that initialization tasks are only completed once. However, the field is mistakenly set to true during static initialization, so the initialization code is never reached.
Example Language:Java (Unsupported language for documentation only)
private boolean initialized = true;
public void someMethod() {
if (!initialized) {
// perform initialization tasks
...
initialized = true;
}
Example 2
The following code intends to limit certain operations to the administrator only.
Example Language:Perl (Unsupported language for documentation only)
$username = GetCurrentUser();
$state = GetStateData($username);
if (defined($state)) {
$uid = ExtractUserID($state);
}
# do stuff
if ($uid == 0) {
DoAdminThings();
}
If the application is unable to extract the state information - say, due to a database timeout - then the $uid variable will not be explicitly set by the programmer. This will cause $uid to be regarded as equivalent to "0" in the conditional, allowing the original user to perform administrator actions. Even if the attacker cannot directly influence the state data, unexpected errors could cause incorrect privileges to be assigned to a user just by accident.
Example 3
The following code intends to concatenate a string to a variable and print the string.
Example Language:C
char str[20];
strcat(str, "hello world");
printf("%s", str);
This might seem innocent enough, but str was not initialized, so it contains random memory. As a result, str[0] might not contain the null terminator, so the copy might start at an offset other than 0. The consequences can vary, depending on the underlying memory.
If a null terminator is found before str[8], then some bytes of random garbage will be printed before the "hello world" string. The memory might contain sensitive information from previous uses, such as a password (which might occur as a result of CWE-14 or CWE-244). In this example, it might not be a big deal, but consider what could happen if large amounts of memory are printed out before the null terminator is found.
If a null terminator isn't found before str[8], then a buffer overflow could occur, since strcat will first look for the null terminator, then copy 12 bytes starting with that location. Alternately, a buffer over-read might occur (CWE-126) if a null terminator isn't found before the end of the memory segment is reached, leading to a segmentation fault and crash.
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
assigned_to_pointer_to_const |
Assigning the address of a partially initialized variable to some pointer-to-const |
None |
False |
pass_as_pointer_to_const_param |
Passing uninitialized variable by pointer as function parameter with pointer-to-const type |
None |
False |
possible_invalid_call_argument |
Call to {} with string buffer argument {} that possibly has no valid null delimiter character. |
None |
False |
possible_return_value_uninit |
Function return value is potentially not initialized |
None |
False |
possible_uninit |
Use of possibly uninitialized variable |
None |
False |
possibly_initialized |
Use of possibly uninitialized variable (previous call {node0} might have initialized the variable) |
None |
False |
return_value_uninit |
Function return value is not initialized |
None |
False |
uninit |
Use of uninitialized variable |
None |
False |
Options¶
This rule shares the following common options: 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
additional_local_array_check¶
additional_local_array_check : bool = True
int example()
{
int a[10];
int b[20];
int uninit_var;
for (int i = 0; i < 10; ++i)
{
L1: a[i] = uninit_var; // use of uninit_var reported
b[i] = i;
}
int result = a[3]; // not reported, since already reported at L1
result += b[15]; // reported; c[] is not (completely) initialized
return result;
}
assume_globals_are_initialized¶
assume_globals_are_initialized : bool = True
check_array_access_with_unknown_index¶
check_array_access_with_unknown_index : bool = False
a[i] with non-literal index
i should be checked as well.
concat_operations¶
concat_operations
Names of buffer-concatenating functions being relevant as call targets for this check, with the position of the argument pointing to the destination buffer, and the position of the argument that references the buffer that should be appended at the end of the destination buffer.Type: dict[bauhaus.analysis.config.QualifiedName, typing.Tuple[int, int]]
Default:
{ 'strcat': (0, 1) }
copy_operations¶
copy_operations
Names of buffer copy functions being relevant as call targets for this check, with the position of the destination argument and the source argument of the buffer copy operation.Type: dict[bauhaus.analysis.config.QualifiedName, typing.Tuple[int, int]]
Default:
{ 'strcpy': (0, 1) }
delimiter_of_arguments¶
delimiter_of_arguments
Names of functions being relevant as call targets for this check, with the position of parameters whose referenced buffers should be checked for being properly terminated by a null terminator.Type: dict[bauhaus.analysis.config.QualifiedName, set[int]]
Default:
{ 'strcat': {0, 1}, 'strchr': {0}, 'strcmp': {0, 1}, 'strcoll': {0, 1}, 'strcpy': {1}, 'strcspn': {0, 1}, 'strlen': {0}, 'strncat': {0, 1}, 'strpbrk': {0, 1}, 'strrchr': {0}, 'strspn': {0, 1}, 'strstr': {0, 1}, 'strtok': {0, 1}, 'wcscat': {0, 1}, 'wcschr': {0}, 'wcscmp': {0, 1}, 'wcscpy': {1}, 'wcscspn': {0, 1}, 'wcslen': {0}, 'wcsncat': {0, 1}, 'wcsrchr': {0}, 'wcsspn': {0, 1} }
exclude_from_pointer_to_const_param_check¶
exclude_from_pointer_to_const_param_check : set[bauhaus.analysis.config.QualifiedName] = {'__builtin_object_size'}
exclude_warnings_for_unknown_arguments¶
exclude_warnings_for_unknown_arguments : bool = False
ignore_calls_in_functions¶
ignore_calls_in_functions : set[bauhaus.analysis.config.QualifiedName] = set()
track_conditional_initialization¶
track_conditional_initialization : bool = True
use_semantic_analysis¶
use_semantic_analysis : bool = True
writing_into_pointer_to_const¶
writing_into_pointer_to_const
Names of routines (mapping to parameter index, starting at 0) having a parameter declared as pointer-to-const yet they are still writing into the pointee.Type: dict[bauhaus.analysis.config.QualifiedName, int]
Default:
{ 'cudaMemcpyToSymbol': 0 }