From 85969fdaa7c7e4580aa2b19bd4921a0a25b6856c Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Fri, 29 Nov 2024 13:41:54 -0500 Subject: [PATCH] level: Fix integer overflow when filling LevelMeta The level in GstAudioLevelMeta is represented as a signed 8bit value from 0 to 127 (with 127 meaning silence). When converting from double, make sure to clip the value, this also prevent integer overflow in the conversion. This fixes an issue where a lower then -127db is reported and random level with near silent streams (due to integer overflow). Fixes #4068 Part-of: --- subprojects/gst-plugins-good/gst/level/gstlevel.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/subprojects/gst-plugins-good/gst/level/gstlevel.c b/subprojects/gst-plugins-good/gst/level/gstlevel.c index d868fc9aa9..c9746e79d6 100644 --- a/subprojects/gst-plugins-good/gst/level/gstlevel.c +++ b/subprojects/gst-plugins-good/gst/level/gstlevel.c @@ -731,8 +731,18 @@ gst_level_transform_ip (GstBaseTransform * trans, GstBuffer * in) if (filter->audio_level_meta) { gdouble RMS = sqrt (CS_tot / num_int_samples); gdouble RMSdB = 20 * log10 (RMS + EPSILON); + guint8 level; - gst_level_rtp_audio_level_meta (filter, in, -RMSdB); + /* -127db is considered silent in audio level meta, clip anything below and + * avoid possible integer overflow */ + if (RMSdB < -127.0) + level = 127; + else if (RMSdB > 0.0) + level = 0; + else + level = -RMSdB; + + gst_level_rtp_audio_level_meta (filter, in, level); } GST_OBJECT_UNLOCK (filter);