A file is checked with access and opened by name afterwards, so it may have changed in between

tx0/c:check-then-openrecommendedwarningsecurity, cwe-367

What it finds

if (access(path, W_OK) == 0) f = fopen(path, "w");: between the check and the open, another process can replace the file with a link to one the program should not touch. Opening the file and checking what was opened, with the program's own permissions, leaves no gap.

The open must come later in the same function with the same path, written the same way and without side effects.

From the rule's source, check_then_open.rs.

Example from the rule's tests
#include <stdio.h>#include <unistd.h> FILE *open_checked(const char *path){  if (access(path, W_OK) != 0)tx0 reports: the file is checked here and opened by name later    return NULL;  return fopen(path, "w");}
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:check-then-open" = "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:check-then-open] reason="..."

tx0 explain tx0/c:check-then-open shows the same in the terminal. Found a false positive? Report it.