...
This noncompliant code example uses the assert()
macro to verify that memory allocation succeeded. Because memory availability depends on the overall state of the system and can become exhausted at any point during a process lifetime, a robust program must be prepared to gracefully handle and recover from its exhaustion. Consequently, using the assert()
macro to verify that a memory allocation succeeded would be inappropriate because doing so might lead to an abrupt termination of the process, opening the possibility of a denial-of-service attack. See also MEM11-C. Do not assume infinite heap space and void MEM32-C. Detect and handle memory allocation errors.
Code Block | ||||
---|---|---|---|---|
| ||||
char *dupstring(const char *c_str) { size_t len; char *dup; len = strlen(c_str); dup = (char *)malloc(len + 1); assert(NULL != dup); memcpy(dup, c_str, len + 1); return dup; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
char *dupstring(const char *c_str) { size_t len; char *dup; len = strlen(c_str); dup = (char*)malloc(len + 1); /* detectDetect and handle memory allocation error */ if (NULL == dup) { return NULL; } memcpy(dup, c_str, len + 1); return dup; } |
...
Assertions are a valuable diagnostic tool for finding and eliminating software defects that may result in vulnerabilities. The absence of assertions, however, does not mean that code is incorrect.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MSC11-C |
Low |
Unlikely |
High | P1 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| LANG.FUNCS.ASSERTS | Not enough assertions | ||||||
| ASSERT_SIDE_EFFECT | Can detect the specific instance where assertion contains an operation/function call that may have a side effect | |||||||
Parasoft C/C++test |
| CERT_C-MSC11-a | Assert liberally to document internal assumptions and invariants |
Related Vulnerabilities
Search for for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
CERT C Secure Coding Standard | ERR00-C. Adopt and implement a consistent and comprehensive error-handling policy |
SEI CERT C++ |
Coding Standard | VOID MSC11-CPP. Incorporate diagnostic tests using assertions |
MITRE CWE | CWE-190, Reachable assertion |
...
...