From 7c71181c27c3ccedf8f183b041dd7656e764cdb3 Mon Sep 17 00:00:00 2001 From: YongDo-Hyun Date: Sat, 27 Dec 2025 13:15:25 +0300 Subject: fix: improve exception handling in ozlibstream::close to prevent masking errors Signed-off-by: YongDo-Hyun --- src/io/ozlibstream.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp index 5f479f83f8..ba6187645c 100644 --- a/src/io/ozlibstream.cpp +++ b/src/io/ozlibstream.cpp @@ -95,27 +95,33 @@ int deflate_streambuf::sync() void ozlibstream::close() { + // Capture the exception mask so we can restore it if close() fails. std::ios_base::iostate old_ex = exceptions(); + bool close_failed = false; try { buf.close(); } catch(...) { - // Setting the stream state while exceptions are enabled may cause - // `setstate` to throw an `ios_base::failure` which would replace - // the original exception. Temporarily disable exceptions on this - // stream, set the `badbit`, then restore the exception mask. - try { - exceptions(std::ios_base::goodbit); - setstate(std::ios_base::badbit); - } - catch(...) { - // If anything unexpected happens while setting state, swallow - // it — we don't want this to throw here. - } - exceptions(old_ex); + close_failed = true; + } + if(!close_failed) + return; + + // Setting the stream state while exceptions are enabled may cause + // `setstate` to throw an `ios_base::failure` which would replace + // the original exception. Temporarily disable exceptions on this + // stream, set the `badbit`, then restore the exception mask. + try { + exceptions(std::ios_base::goodbit); + setstate(std::ios_base::badbit); + } + catch(...) { + // If anything unexpected happens while setting state, swallow + // it — we don't want this to throw here. } + exceptions(old_ex); } } -- cgit 0.0.5-2-1-g0f52