A pointer from c_str() or data() of a temporary string is kept, and the string is destroyed at the end of the statement

tx0/cpp:pointer-into-temporary-stringdefaulterrorreliability/memory-safetydepth fastbug, cwe-416

What it finds

const char *p = std::to_string(n).c_str(); takes a pointer into a temporary std::string that is destroyed when the statement ends; p dangles from the next line on. The string needs a name that lives as long as the pointer is used. Temporaries counted are calls of std::string, std::to_string and std::format, of .str() and .substr(), and concatenations with +.

From the rule's source, pointer_into_temporary_string.rs.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/cpp:pointer-into-temporary-string" = "off"

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/cpp:pointer-into-temporary-string] reason="..."

tx0 explain tx0/cpp:pointer-into-temporary-string shows the same in the terminal. Found a false positive? Report it.