From patchwork Thu Sep 7 21:24:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 43646 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4e27:b0:149:dfde:5c0a with SMTP id gk39csp90890pzb; Thu, 7 Sep 2023 14:24:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEi2GBL9EWPjsNPKQ6tNrIbL779xfF/lA6PhiLvyxlqBpzM0xX3aU0nRckIldxbHBzhJarS X-Received: by 2002:a2e:9dcf:0:b0:2bc:c650:81b with SMTP id x15-20020a2e9dcf000000b002bcc650081bmr312962ljj.15.1694121895282; Thu, 07 Sep 2023 14:24:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694121895; cv=none; d=google.com; s=arc-20160816; b=vkPhD5rAuG7IGLMVrj3YXt4XA0BUJ4fJvvzPkZcJTOz55g0YKDrYjIVl1E9MeWvLh6 CMaLhgGTjcIqq/1Albn/xKMpmzNgZaGEw2epPVaJCHqkJ6xShi10Ph1repnCRyJYFxY2 ysbjMJ3VRzrkp3CDcCADpIsQTUQydEbQw5kdMA2rRMh7jg0bAQR2FG1Uyn67uW/XOb9Z 33Ny18whs7pbw6WzX0OpjoIwX2iIeXPcK90GOWhGE3GXd3yIIX5+5ALIOus9bj24jK3F 3JkoREdNIO2evn5FrXGgHNha99N0uSEwTOfLjlGe/LJhr10H3YUqwcSuJeqDEzXm9WCZ B6RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to; bh=PzulguZNIDXG8DPRwQTszdP5DkPWr0yYYDrUQ37KXkQ=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=O4bpcOJ0+BR5dk0t7MfziQSgxT1cUtRSKQwqHfHzRJTLwRRQZu7U0U2h1MRbqu021u dnizO0OpXYZLd9pckToBldMSAP0P0zkAgM1NCVwe8Wn1KoGbKoTBHhO48wgYF1A6U6cw us22NqnL/bv/EIANh7yI+I0TLINlPbNZu0HadpZXoVX4kCpYpg7phel5AtOmzUbYUEHW M2BU2IVXMvALwG2hdNI/mZe776fStYKshAcxiDnJm0EpRlCvRl4wUOT8xBlhp/tQ4xVd j/Xra+F/K7A3ppQcWCbHbI35Wgt4ht10sbcd0tRU03VYR0wuf8D5XCoG8PdNoDNG4r7o 6IKg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cw13-20020a170906c78d00b009a9f5afdab7si161515ejb.647.2023.09.07.14.24.54; Thu, 07 Sep 2023 14:24:55 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7D12868C6AE; Fri, 8 Sep 2023 00:24:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5E53668C81A for ; Fri, 8 Sep 2023 00:24:44 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 91613E0006 for ; Thu, 7 Sep 2023 21:24:43 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 7 Sep 2023 23:24:42 +0200 Message-Id: <20230907212442.12426-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH] avcodec/celp_math: avoid overflow in shift X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 1hCaJFIPQY2y by making gain unsigned we have 1 bit more available alternatively we can clip twice as in the g729 reference Fixes: left shift of 23404 by 17 places cannot be represented in type 'int' Fixes: 61728/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_ACELP_KELVIN_fuzzer-6280412547383296 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/celp_math.h | 2 +- libavcodec/g729postfilter.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/celp_math.h b/libavcodec/celp_math.h index 18888a425d..99a0470719 100644 --- a/libavcodec/celp_math.h +++ b/libavcodec/celp_math.h @@ -78,7 +78,7 @@ int64_t ff_dot_product(const int16_t *a, const int16_t *b, int length); * * @return value << offset, if offset>=0; value >> -offset - otherwise */ -static inline int bidir_sal(int value, int offset) +static inline unsigned bidir_sal(unsigned value, int offset) { if(offset < 0) return value >> -offset; else return value << offset; diff --git a/libavcodec/g729postfilter.c b/libavcodec/g729postfilter.c index 26e937f0ba..382db92432 100644 --- a/libavcodec/g729postfilter.c +++ b/libavcodec/g729postfilter.c @@ -581,7 +581,7 @@ void ff_g729_postfilter(AudioDSPContext *adsp, int16_t* ht_prev_data, int* voici int16_t ff_g729_adaptive_gain_control(int gain_before, int gain_after, int16_t *speech, int subframe_size, int16_t gain_prev) { - int gain; // (3.12) + unsigned gain; // (3.12) int n; int exp_before, exp_after; @@ -603,7 +603,7 @@ int16_t ff_g729_adaptive_gain_control(int gain_before, int gain_after, int16_t * gain = ((gain_before - gain_after) << 14) / gain_after + 0x4000; gain = bidir_sal(gain, exp_after - exp_before); } - gain = av_clip_int16(gain); + gain = FFMIN(gain, 32767); gain = (gain * G729_AGC_FAC1 + 0x4000) >> 15; // gain * (1-0.9875) } else gain = 0;