From a87ba5ae9ae93b037a6674b60de9ed07cd1f8d96 Mon Sep 17 00:00:00 2001 From: lani_karrot Date: Sun, 22 Nov 2026 17:26:01 +0900 Subject: [PATCH 1/2] fix: memory leak in serde --- common/include/serde.hpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/common/include/serde.hpp b/common/include/serde.hpp index 02c2fc169..f384a2e8f 100644 --- a/common/include/serde.hpp +++ b/common/include/serde.hpp @@ -161,7 +161,6 @@ struct serde { /// @copydoc serde::deserialize void deserialize(std::istream& is, std::string* items, unsigned num) const { unsigned i = 0; - bool failure = false; try { for (; i < num; i++) { uint32_t length; @@ -176,14 +175,18 @@ struct serde { new (&items[i]) std::string(std::move(str)); } } catch (std::istream::failure&) { - failure = true; + // the stream sets its state before throwing, so this is reported below + } catch (...) { + for (unsigned j = 0; j < i; ++j) { + items[j].~basic_string(); + } + throw; } - if (failure || !is.good()) { - // clean up what we've already allocated + if (!is.good()) { for (unsigned j = 0; j < i; ++j) { items[j].~basic_string(); } - throw std::runtime_error("error reading from std::istream at item " + std::to_string(i)); + throw std::runtime_error("error reading from std::istream at item " + std::to_string(i)); } } From 580a26eab581a64c3ef91fb689feb83ad2704a44 Mon Sep 17 00:00:00 2001 From: lani_karrot Date: Mon, 23 Nov 2026 18:04:17 +0900 Subject: [PATCH 2/2] fix: missing memory leak --- common/include/serde.hpp | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/common/include/serde.hpp b/common/include/serde.hpp index f384a2e8f..cc57e20bf 100644 --- a/common/include/serde.hpp +++ b/common/include/serde.hpp @@ -211,25 +211,32 @@ struct serde { size_t bytes_read = 0; unsigned i = 0; bool failure = false; - for (; i < num && !failure; ++i) { - uint32_t length; - if (bytes_read + sizeof(length) > capacity) { - bytes_read += sizeof(length); // we'll use this to report the error - failure = true; - break; + try { + for (; i < num && !failure; ++i) { + uint32_t length; + if (bytes_read + sizeof(length) > capacity) { + bytes_read += sizeof(length); // we'll use this to report the error + failure = true; + break; + } + memcpy(&length, ptr, sizeof(length)); + ptr = static_cast(ptr) + sizeof(uint32_t); + bytes_read += sizeof(length); + + if (bytes_read + length > capacity) { + bytes_read += length; // we'll use this to report the error + failure = true; + break; + } + new (&items[i]) std::string(static_cast(ptr), length); + ptr = static_cast(ptr) + length; + bytes_read += length; } - memcpy(&length, ptr, sizeof(length)); - ptr = static_cast(ptr) + sizeof(uint32_t); - bytes_read += sizeof(length); - - if (bytes_read + length > capacity) { - bytes_read += length; // we'll use this to report the error - failure = true; - break; + } catch (...) { + for (unsigned j = 0; j < i; ++j) { + items[j].~basic_string(); } - new (&items[i]) std::string(static_cast(ptr), length); - ptr = static_cast(ptr) + length; - bytes_read += length; + throw; } if (failure) {