Memory for a copy of a string is allocated without room for its terminating zero
What it finds
malloc(strlen(s)): the length of a string does not count its terminating zero, so a copy of s into the new memory writes one byte past its end. strlen(s) + 1 is the size of a copy.
Sometimes the memory is meant for the characters alone, without the zero, which is why the rule is not in the default set.
From the rule's source, malloc_strlen.rs.
#include <stdlib.h>#include <string.h> char *copy(const char *s){ char *p = malloc(strlen(s));tx0 reports: strlen(s) leaves no room for the terminating zero char *q = malloc(strlen(s) + 1); char *r = malloc((strlen(s)));tx0 reports: leaves no room for the terminating zero free(q); free(r); return p;} 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:
[rules]
"tx0/c:malloc-strlen" = "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:malloc-strlen] reason="..."tx0 explain tx0/c:malloc-strlen shows the same in the terminal. Found a false positive? Report it.