...
Function-like macros should not be invoked without all of their arguments | Bug | cwe, misra, preprocessor | This is a constraint error, but preprocessors have been known to ignore this problem. Each argument in a function-like macro must consist of at least one preprocessing token otherwise the behaviour is undefined. See | ||||
Stack allocated memory should not be freed | Bug | unpredictable | Stack allocated memory, like memory allocated with the functions alloca, _alloca, _malloca, __builtin_alloca, is automatically released at the end of the function, and should not be released with free. Explicitly free-ing such memory results in undefined behavior. Noncompliant Code Example
| ||||
Closed resources should not be accessed | Bug | cert | Using the value of a pointer to a FILE object after the associated file is closed is undefined behavior. Noncompliant Code Example
Compliant Solution
See | ||||
Dynamically allocated memory should be released | Memory allocated dynamically with calloc(...), malloc(...), realloc(...) or new should be released when it's not needed anymore. Failure to do so will result in a memory leak that could bring the box to its knees. This rule raises an issue when memory is allocated and not freed in the same function. Allocated memory is ignored if a pointer to it is returned to the caller or stored in a structure that's external to the function. Noncompliant Code Example
| ||||||
Freed memory should not be used | Once a block of memory has been freed, it becomes available for other memory requests. Whether it's re-used immediately, some time later, or not at all is random, and may vary based on load. Because of that randomness, tests may pass when running locally, but the odds are that such code will fail spectacularly in production by returning strange values, executing unexpected code, or causing a program crash. Noncompliant Code Example
See MITRE, CWE-416 - Use After Free | ||||||