Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upBufWriter is not panic-safe #30888
Comments
huonw
added
I-nominated
T-libs
labels
Jan 14, 2016
This comment has been minimized.
This comment has been minimized.
|
Note that Rust is normally referring to "memory safety" when talking about safety, so the issue here isn't in conflict with the related discussions. It's definitely a bug in |
This comment has been minimized.
This comment has been minimized.
If your
This is not quite the case - What's specifically the expected behavior here? Is the For reference, Java's |
This comment has been minimized.
This comment has been minimized.
Without the But with
I think we need some guideline like: If a method call panics, it's a bug to call any additional methods on the same object. The Java Essentially, the Rust world needs to decide between a Java/C++-style "strong exception safety guarantee" (=failing method call should not have any side effects) or a new "no-use-after-panic guarantee" (=after a failing method call, no additional method calls should occur). Some types (like BufWriter) can be compatible with at most one of the guarantees -- strong exception safety requires that a panicking inner.write call has no side effects; no-use-after-panic requires that a panicking inner.write call has the side effect of suppressing the buffer flush in the The |
dgrunwald commentedJan 14, 2016
There is a panic safety issue in
BufWriter: after ainner.write()call panics, theDropimpl ofBufWritercallsinner.write()again, which means the buffer contents are potentially written twice. This may cause an application to overwrite parts of a file that it did not mean to overwrite (in a DB engine written in Rust, this could cause unrecoverable data corruption!).Demonstration: https://play.rust-lang.org/?gist=9991550d3efb38c93df4&version=stable
The expected output of the demo program is
File contents: aBBccc, the actual output is:File contents: aBBBBcMore generally, we need a story for panic safety in Rust.
My takeaway from the related discussions (e.g. RFC 1236, #27719, the RecoverSafe trait) was that only
unsafecode andDropimpls should have to worry about panic safety. The demo app contains none of these, so I'd consider this a bug inimpl Drop for BufWriter. (otherwise allWriteimplementations would need to provide the strong exception safety guarantee?)Solution:
BufWritercould use temporarily mark the buffer as empty during theinner.writecalls; so that theDropimpl doesn't do anything after a panic.However, this doesn't help if the panic occurs during a
bufWriter.get_mut().write()call...