...
In this compliant solution, the memory is freed after its final use, and the pointer is zeroed in compliance with MEM01-C. Store a new value in pointers immediately after free():
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
/* Print usage */
return EXIT_FAILURE;
}
char *return_val = 0;
const size_t bufsize = strlen(argv[1]) + 1;
char *buf = (char *)malloc(bufsize);
if (!buf) {
return EXIT_FAILURE;
}
/* ... */
return_val = strncpy(buf, argv[1], bufsize);
if (return_val) {
free(buf);
return EXIT_FAILURE;
}
/* ... */
free(buf);
return EXIT_SUCCESS;
}
|
Noncompliant Code Example
...