Skip to content

Latest commit

 

History

History
44 lines (34 loc) · 1.24 KB

File metadata and controls

44 lines (34 loc) · 1.24 KB

incompatibleFileOpen

Message: The file 'a.txt' is opened for read and write access at the same time on different streams
Category: Correctness
Severity: Warning
Language: C/C++

Description

The same filename is opened for reading on one stream while another stream already has it open for writing (or vice versa).

Motivation

Two independent streams onto the same file, one reading and one writing, can observe an inconsistent view of the file's contents depending on buffering and timing - a portability and correctness hazard that's easy to introduce without noticing, since each stream on its own looks fine.

How to fix

Before:

#include <cstdio>
void f() {
    FILE *f1 = fopen("a.txt", "w");
    FILE *f2 = fopen("a.txt", "r"); // <- 'a.txt' is already open for writing
}

After:

#include <cstdio>
void f() {
    FILE *f1 = fopen("a.txt", "r");
    if (f1) fclose(f1);
}

Related checkers