putenv is given a local array, which the environment keeps using after the function returns

tx0/c:putenv-local-bufferrecommendedwarningbug, cwe-562

What it finds

char buf[64]; snprintf(buf, ...); putenv(buf);: putenv does not copy the string but makes it part of the environment, so when the function returns the environment points into stack memory that later calls overwrite. setenv copies.

From the rule's source, putenv_local_buffer.rs.

Example from the rule's tests
#include <stdio.h>#include <stdlib.h> static char kept[64]; void set(const char *value){  char buf[64];  snprintf(buf, sizeof(buf), "HOME=%s", value);  putenv(buf);tx0 reports: the environment keeps pointing into the local array buf  snprintf(kept, sizeof(kept), "HOME=%s", value);  putenv(kept);}
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:putenv-local-buffer" = "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:putenv-local-buffer] reason="..."

tx0 explain tx0/c:putenv-local-buffer shows the same in the terminal. Found a false positive? Report it.