From ff9ccc21b8c66008a99f07b36cccb1a952a340ae Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 28 Jun 2022 14:47:39 +0200 Subject: [PATCH] Properly restore cork state when changed These streams both need to change the corking state temporarily, but it is important it is restored to the previous state or things might get messed up. For the zlib stream it would just leave things uncorked, which still works but is less efficient. But for the TLS stream it might make things very unresponsive as the corking might be left on permanently, delaying packets indefinitely. --- common/rdr/TLSOutStream.cxx | 7 ++++++- common/rdr/ZlibOutStream.cxx | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/common/rdr/TLSOutStream.cxx b/common/rdr/TLSOutStream.cxx index dc6b55aa..63bdeef1 100644 --- a/common/rdr/TLSOutStream.cxx +++ b/common/rdr/TLSOutStream.cxx @@ -124,13 +124,18 @@ void TLSOutStream::cork(bool enable) void TLSOutStream::overrun(size_t needed) { + bool oldCorked; + if (needed > bufSize) throw Exception("TLSOutStream overrun: buffer size exceeded"); // A cork might prevent the flush, so disable it temporarily + oldCorked = corked; corked = false; + flush(); - corked = true; + + corked = oldCorked; } size_t TLSOutStream::writeTLS(const U8* data, size_t length) diff --git a/common/rdr/ZlibOutStream.cxx b/common/rdr/ZlibOutStream.cxx index 0d4d39a7..1ff234ce 100644 --- a/common/rdr/ZlibOutStream.cxx +++ b/common/rdr/ZlibOutStream.cxx @@ -129,11 +129,17 @@ void ZlibOutStream::overrun(size_t needed) checkCompressionLevel(); while (avail() < needed) { + bool oldCorked; + // use corked to make zlib a bit more efficient since we're not trying // to end the stream here, just make some room + + oldCorked = corked; corked = true; + flush(); - corked = false; + + corked = oldCorked; } } -- 2.39.5