Skip to content

Commit 40b9e46

Browse files
bors[bot]tromey
andauthored
Merge #517
517: Fix RAIIFile buglet r=philberty a=tromey RAIIFile's implementation of operator= can leak a file, because it doesn't handle the case where the assignee holds an open file handle. Co-authored-by: Tom Tromey <[email protected]>
2 parents f2d9faf + 2fb749c commit 40b9e46

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

gcc/rust/lex/rust-lex.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ struct RAIIFile
1515
private:
1616
FILE *file;
1717

18+
void close ()
19+
{
20+
if (file != nullptr && file != stdin)
21+
fclose (file);
22+
}
23+
1824
public:
1925
RAIIFile (const char *filename)
2026
{
@@ -31,17 +37,14 @@ struct RAIIFile
3137
RAIIFile (RAIIFile &&other) : file (other.file) { other.file = nullptr; }
3238
RAIIFile &operator= (RAIIFile &&other)
3339
{
40+
close ();
3441
file = other.file;
3542
other.file = nullptr;
3643

3744
return *this;
3845
}
3946

40-
~RAIIFile ()
41-
{
42-
if (file != nullptr && file != stdin)
43-
fclose (file);
44-
}
47+
~RAIIFile () { close (); }
4548

4649
FILE *get_raw () { return file; }
4750
};

0 commit comments

Comments
 (0)