From 24f5914d1838f930bcde75bdffa64b1236a869c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 20 Aug 2023 18:08:52 +0200 Subject: [PATCH] LibAudio: Prevent overflow in QOA LMS prediction --- Userland/Libraries/LibAudio/QOATypes.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibAudio/QOATypes.cpp b/Userland/Libraries/LibAudio/QOATypes.cpp index 7ebdac655f..eb239ddf1b 100644 --- a/Userland/Libraries/LibAudio/QOATypes.cpp +++ b/Userland/Libraries/LibAudio/QOATypes.cpp @@ -38,10 +38,11 @@ LMSState::LMSState(u64 history_packed, u64 weights_packed) i32 LMSState::predict() const { - i32 prediction = 0; + // The spec specifies that overflows are not allowed, but we do a safe thing anyways. + Checked prediction = 0; for (size_t i = 0; i < lms_history; ++i) - prediction += history[i] * weights[i]; - return prediction >> 13; + prediction.saturating_add(Checked::saturating_mul(history[i], weights[i])); + return prediction.value() >> 13; } void LMSState::update(i32 sample, i32 residual)