diff options
author | Tom Tromey <tom@tromey.com> | 2021-06-21 21:55:57 -0600 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2021-06-22 13:43:08 -0600 |
commit | 2fb749c5f438fdc77cbc7336b5a8def1fa8c1581 (patch) | |
tree | eeb22db8d02f7aad5448d39986f3c81c251a6c21 | |
parent | f2d9faf3862a75247adbd6c233c889010902a34b (diff) | |
download | gcc-2fb749c5f438fdc77cbc7336b5a8def1fa8c1581.zip gcc-2fb749c5f438fdc77cbc7336b5a8def1fa8c1581.tar.gz gcc-2fb749c5f438fdc77cbc7336b5a8def1fa8c1581.tar.bz2 |
Fix RAIIFile buglet
RAIIFile's implementation of operator= can leak a file, because it
doesn't handle the case where the assignee holds an open file handle.
-rw-r--r-- | gcc/rust/lex/rust-lex.h | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h index 00f9d21..ba37c63 100644 --- a/gcc/rust/lex/rust-lex.h +++ b/gcc/rust/lex/rust-lex.h @@ -15,6 +15,12 @@ struct RAIIFile private: FILE *file; + void close () + { + if (file != nullptr && file != stdin) + fclose (file); + } + public: RAIIFile (const char *filename) { @@ -31,17 +37,14 @@ public: RAIIFile (RAIIFile &&other) : file (other.file) { other.file = nullptr; } RAIIFile &operator= (RAIIFile &&other) { + close (); file = other.file; other.file = nullptr; return *this; } - ~RAIIFile () - { - if (file != nullptr && file != stdin) - fclose (file); - } + ~RAIIFile () { close (); } FILE *get_raw () { return file; } }; |