OpenOptions chain combines options that contradict each other
ID |
rust.correctness.file_open_options_consistent |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Misuse |
Language |
Rust |
Description
This OpenOptions chain asks for a combination the file system cannot honour, so open() fails with InvalidInput at run time. truncate, create and create_new all require write access, and truncating a file opened for appending is contradictory. Add .write(true) or drop the option that does not belong.
Rationale
This OpenOptions chain asks for a combination the file system cannot honour, so open() fails with InvalidInput at run time. truncate, create and create_new all require write access, and truncating a file opened for appending is contradictory. Add .write(true) or drop the option that does not belong.
The following code illustrates the pattern detected by this rule:
use std::fs::{File, OpenOptions};
use std::io::Result;
fn truncate_without_write() -> Result<File> {
// FLAGGED: OpenOptions chain combines options that contradict each other
OpenOptions::new().read(true).truncate(true).open("data.log")
}