Memory is cleared with memset right before it is freed, which the compiler may drop

tx0/c:erased-before-freerecommendedwarningsecurity, cwe-14

What it finds

memset(key, 0, len); free(key);: the cleared memory is never read again, so the compiler may remove the memset as a dead store and the secret stays on the heap. explicit_bzero or memset_s are kept.

From the rule's source, erased_before_free.rs.

Example from the rule's tests
#include <stdlib.h>#include <string.h> void drop(char *key, unsigned long n){  memset(key, 0, n);tx0 reports: the compiler may remove this memset  free(key);} void kept(char *key, unsigned long n){  memset(key, 1, n);  free(key);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:erased-before-free" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:erased-before-free] reason="..."

tx0 explain tx0/c:erased-before-free shows the same in the terminal. Found a false positive? Report it.