A file is created after a separate check that it does not exist, leaving a gap for a race
What it finds
``go if _, err := os.Stat(path); os.IsNotExist(err) { f, err := os.Create(path) ` Between the check and the creation another process can create the file, or put a symbolic link there, and os.Create then truncates or writes through it. Opening with os.O_CREATE|os.O_EXCL` checks and creates in one step and fails if the file exists.
Reported is a call of os.Create or os.WriteFile, or of os.OpenFile whose flags do not mention O_EXCL, on the same path expression, without calls, as an earlier call of os.Stat or os.Lstat in the same function, when the function tests the error of the check with os.IsNotExist or compares it with fs.ErrNotExist or os.ErrNotExist.
From the rule's source, stat_then_create.rs.
package p import "os" func ensure(path string, data []byte) error { if _, err := os.Stat(path); os.IsNotExist(err) { return os.WriteFile(path, data, 0o600)tx0 reports: os.WriteFile creates path after a separate check that it is missing } return nil} func exclusive(path string) error { if _, err := os.Stat(path); os.IsNotExist(err) { f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) if err != nil { return err } return f.Close() } return nil} 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/go:stat-then-create" = "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/go:stat-then-create] reason="..."tx0 explain tx0/go:stat-then-create shows the same in the terminal. Found a false positive? Report it.