...
In this example, sensitive information stored in the dynamically allocated memory referenced by secret is copied to the dynamically allocated buffer, new_secret, which is processed and eventually deallocated by a call to free(). Because the memory is not cleared, it may be reallocated to another section of the program where the information stored in new_secret may be unintentionally leaked.
| Code Block | ||||
|---|---|---|---|---|
| ||||
char *secret;
/* initialize secret */
char *new_secret;
size_t size = strlen(secret);
if (size == SIZE_MAX) {
/* Handle error */
}
new_secret = (char *)malloc(size+1);
if (!new_secret) {
/* Handle error */
}
strcpy(new_secret, secret);
/* Process new_secret... */
free(new_secret);
new_secret = NULL;
|
...