...
Code Block | ||
---|---|---|
| ||
x = malloc (number * sizeof(int)); if (x == NULL) { /* Handle Allocation Error */ } /* ... */ if (error_conditon == 1) { /* Handle Error Condition*/ free(x); } /* ... */ free(x); |
Compliant Solution
Wiki Markup |
---|
Only free a pointer to dynamic memory referred to by {{x}} once. This is accomplished by removing the call to {{free()}} in the section of code executed when {{error_condition}} is true. Note that this solution checks for numeric overflow \[[INT32-C. Ensure that integer operations do not result in an overflow]\]. |
Code Block | ||
---|---|---|
| ||
if (sizeof(int) > SIZE_MAX/number) { /* handle overflow */ } x = malloc (number * sizeof(int)); if (x == NULL) { /* Handle Allocation Error */ } /* ... */ if (error_conditon == 1) { /* Handle Error Condition*/ } /* ... */ free(x); |
...