From 8b3e888920304a6ff08355136bb40a87d5e675d1 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sat, 30 Aug 2025 08:01:45 +0200 Subject: [PATCH] LibMedia: Don't store view into deleted ByteString DecoderError::formatted() made a ByteString, took a view into it, dropped the ByteString, then propagated the (now invalid) view back to the caller as an error. Since the object has to keep the ByteString alive, keep it as a variant to make sure the "happy" path with no alloc remains alloc-free. --- Libraries/LibMedia/DecoderError.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Libraries/LibMedia/DecoderError.h b/Libraries/LibMedia/DecoderError.h index 95cbb9d5162..84818359467 100644 --- a/Libraries/LibMedia/DecoderError.h +++ b/Libraries/LibMedia/DecoderError.h @@ -34,7 +34,8 @@ enum class DecoderErrorCategory : u32 { class DecoderError { public: - static DecoderError with_description(DecoderErrorCategory category, StringView description) + template T> + static DecoderError with_description(DecoderErrorCategory category, T description) { return DecoderError(category, description); } @@ -62,25 +63,30 @@ public: } DecoderErrorCategory category() const { return m_category; } - StringView description() const { return m_description; } - StringView string_literal() const { return m_description; } + StringView description() const + { + return m_description.visit([](StringView x) { return x; }, [](ByteString const& x) { return x.view(); }); + } + // For compatibility with AK::Error + StringView string_literal() const { return description(); } private: - DecoderError(DecoderErrorCategory category, ByteString description) + template T> + DecoderError(DecoderErrorCategory category, T description) : m_category(category) , m_description(move(description)) { } DecoderErrorCategory m_category { DecoderErrorCategory::Unknown }; - ByteString m_description; + Variant m_description; }; #define DECODER_TRY(category, expression) \ ({ \ auto&& _result = ((expression)); \ if (_result.is_error()) [[unlikely]] { \ - auto _error_string = _result.release_error().string_literal(); \ + auto _error_string = _result.error().string_literal(); \ return DecoderError::from_source_location( \ ((category)), _error_string, SourceLocation::current()); \ } \