errno is tested after a conversion function without being set to zero before the call

tx0/c:errno-not-clearedrecommendedwarningbug

What it finds

n = strtol(s, &end, 10); if (errno == ERANGE): the conversion functions set errno on failure but never clear it, so a value left from an earlier call is mistaken for an error of this one. errno = 0; before the call makes the test mean something.

The function must test errno after the call and assign it zero nowhere before the call.

From the rule's source, errno_not_cleared.rs.

Example from the rule's tests
#include <errno.h>#include <stdlib.h> long parse(const char *s, char **end){  long n = strtol(s, end, 10);tx0 reports: errno is tested after strtol but not cleared before it  if (errno == ERANGE)    return 0;  return n;} long checked(const char *s, char **end){  long n;  errno = 0;  n = strtol(s, end, 10);  if (errno == ERANGE)    return 0;  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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:errno-not-cleared" = "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:errno-not-cleared] reason="..."

tx0 explain tx0/c:errno-not-cleared shows the same in the terminal. Found a false positive? Report it.