The condition of a loop calls strlen on a string the loop does not change, once per step

tx0/c:strlen-in-loop-conditionstrictwarningperformance

What it finds

for (i = 0; i < strlen(s); i++): strlen walks the whole string on every step, so the loop takes time quadratic in the length. Computing the length once before the loop is the same when the body does not change the string.

The string must be a variable the file declares that the body does not assign; writes through it are not looked at.

From the rule's source, strlen_in_loop_condition.rs.

Example from the rule's tests
#include <string.h> int spaces(const char *s, char *t){  int n = 0;  unsigned long i;  for (i = 0; i < strlen(s); i++)tx0 reports: strlen runs on every step of the loop    n += s[i] == ' ';  for (i = 0; i < strlen(t); i++)    strcpy(t, "x");  return n;}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:strlen-in-loop-condition" = "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:strlen-in-loop-condition] reason="..."

tx0 explain tx0/c:strlen-in-loop-condition shows the same in the terminal. Found a false positive? Report it.