Review report

bitcoin/bitcoin: PR #36049

5 suggestions

Optional improvements

Link

[suggestion] · Destructor fallback retains an undocumented termination path

Fallback I/O during unwinding can turn a catchable serialization error into std::terminate if the write also fails, but the new comment does not state this behavior.

Location: src/streams.h:692 (head).

Details and suggested fix

The new buffered_writer_destructor_flushes_pending_bytes test requires a write while its simulated serialization exception unwinds. ~BufferedWriter() is implicitly noexcept, so a short write throwing from AutoFile::write_buffer() during that fallback invokes std::terminate instead of reaching the handler. State this consequence explicitly in the class comment, rather than only saying errors cannot be reported. For unwinding, skip fallback I/O when std::uncaught_exceptions() exceeds a count saved at construction and adjust that test case; this preserves fallback on normal exit. Both production callers now flush explicitly, and failed flushes leave no pending bytes. No current production caller triggering this fallback was found.


Link

[suggestion] · Storage write-error handling lacks regression coverage

The fake-stream test cannot detect removal of either storage-level flush, which would move final-buffer write failures back into the non-throwing destructor.

Location: src/node/blockstorage.cpp:1175 (head).

Details and suggested fix

The new failure test calls flush() on its own fake stream, while the blockmanager write/readback tests cannot distinguish successful explicit flushing from destructor fallback. Add deterministic failure coverage for WriteBlock and WriteBlockUndo, injecting a write failure after allocation and successful file opening, with final bytes still buffered. Assert that WriteBlock issues fatalError and returns a null FlatFilePos. For undo, use a fresh undo position, a valid pprev, and an initialized file cursor; assert fatalError, a false result, state.IsError(), and BLOCK_HAVE_UNDO remaining unset. These methods construct their AutoFile directly, so exercising this path needs a fault-injection seam rather than merely making file opening fail.


Link

[suggestion] · Unchecked tmpfile failure can terminate the streams test suite

If std::tmpfile() returns nullptr, the new test buffers a byte before checking the handle and can terminate the streams_tests process under CTest.

Location: src/test/streams_tests.cpp:775 (head).

Details and suggested fix

If std::tmpfile() returns nullptr, AutoFile leaves m_position unset. The first file.tell() then throws while the writer still holds the byte. Its implicitly noexcept destructor calls flush(), which throws again on the null file handle and invokes std::terminate. CTest runs streams_tests with --catch_system_error=no, so this can end that suite's process rather than report the failed file creation normally. Add BOOST_REQUIRE(!file.IsNull()) immediately after creating AutoFile and before constructing BufferedWriter. An actual CI occurrence has not been established.


Link

[suggestion] · The failed-flush postcondition is undocumented

Clearing m_buf_pos deliberately abandons pending bytes on failure, but the API does not document this or the unsafe continuation with AutoFile after a partial write.

Location: src/streams.h:709 (head).

Details and suggested fix

std::exchange prevents destruction from retrying a failed write, so a flush exception leaves no retryable pending data. With obfuscation enabled, AutoFile::write_buffer() transforms the buffer in place before fwrite() and advances m_position only after complete success. Retaining and retrying that buffer would apply the same XOR again, while continuing with new data after a partial write can use a stale obfuscation offset. Add a comment at the exchange explaining why the reset must precede the write, and document that callers must abandon the writer and its destination after a failed flush. Current production callers already close the file and return on failure; no current caller reusing them after failure was found.


Link

[suggestion] · Automatic-flush exception cleanup is not exercised

The failure fixture never fills its buffer, so it does not directly cover no-retry behavior when write() flushes automatically.

Location: src/test/streams_tests.cpp:758 (head).

Details and suggested fix

The fixture writes one byte into the default 64 KiB buffer, so write() never reaches its full-buffer flush. Automatic flushing currently shares flush(), and its reset ordering is therefore covered indirectly, but the test does not exercise exception propagation from write() itself. Extend it with a fresh FailingStream, a one-byte buffer, and writer.write("0000"_hex), without an explicit flush. Check for std::ios_base::failure and exactly one write attempt. This exercises destruction while the automatic write failure unwinds and would catch a future implementation that writes full buffers directly but clears the pending count only after success.


Coverage details