diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-22 21:12:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 21:12:49 +0000 |
commit | 40b9e46abe4782348681a97c996af4c4a090d001 (patch) | |
tree | eeb22db8d02f7aad5448d39986f3c81c251a6c21 /gcc | |
parent | f2d9faf3862a75247adbd6c233c889010902a34b (diff) | |
parent | 2fb749c5f438fdc77cbc7336b5a8def1fa8c1581 (diff) | |
download | gcc-40b9e46abe4782348681a97c996af4c4a090d001.zip gcc-40b9e46abe4782348681a97c996af4c4a090d001.tar.gz gcc-40b9e46abe4782348681a97c996af4c4a090d001.tar.bz2 |
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 <tom@tromey.com>
Diffstat (limited to 'gcc')
-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; } }; |