...
Noncompliant Code Example
Code Block | ||
---|---|---|
| ||
int do_something(void){
// ... definitions ...
obj1 = malloc(...);
if (!obj1){
return -1;
}
obj2 = malloc(...);
if (!obj2){
free(obj1);
return -1;
}
obj3 = malloc(...);
if (!obj3){
free(obj2);
return -1; // Forgot to free obj1 -- Memory leak
}
// ... more code ...
}
|
...
Compliant Solution
Code Block | ||
---|---|---|
| ||
int do_something(void){ // ... definitions ,,, obj1 = malloc(...); if (!obj1){ goto FAIL_OBJ1; } obj2 = malloc(...); if (!obj2){ goto FAIL_OBJ2; } obj3 = malloc(...); if (!obj3){ goto FAIL_OBJ3; } ? // ... more code ... FAIL_OBJ3: free(obj2); FAIL_OBJ2: free(obj1); FAIL_OBJ1: return -1; } |
This code is guaranteed to clean up properly whenever an allocation fails. It is cleaner and prevents rewriting of similar code upon every function error.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MEM86-C | low | probable | medium | P3 | L3 |