From: Samyak Annapureddy <[email protected]>
Date: Sat, 4 Apr 2026 14:15:00 -0700
Subject: [PATCH v4] avformat/vpk: fix division by zero and validate
stream parameters

This patch prevents a division by zero in vpk_read_header by validating
sample_rate, nb_channels, and block_align immediately after they are
read from the header.

While some decoders may catch these invalid values later in the pipeline,
validating them in the demuxer provides defense-in-depth and ensures
that samples_per_block is calculated using safe divisors.

Signed-off-by: Samyak Annapureddy <[email protected]>
---
 libavformat/vpk.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libavformat/vpk.c b/libavformat/vpk.c
index f6270a11ae..3a436214b1 100644
--- a/libavformat/vpk.c
+++ b/libavformat/vpk.c
@@ -58,11 +58,14 @@ static int vpk_read_header(AVFormatContext *s)
     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id    = AV_CODEC_ID_ADPCM_PSX;
     st->codecpar->block_align = avio_rl32(s->pb);
     st->codecpar->sample_rate = avio_rl32(s->pb);
-    if (st->codecpar->sample_rate <= 0 || st->codecpar->block_align <= 0)
-        return AVERROR_INVALIDDATA;
     st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb);
-    if (st->codecpar->ch_layout.nb_channels <= 0)
+
+    if (st->codecpar->sample_rate <= 0 ||
+        st->codecpar->ch_layout.nb_channels <= 0 ||
+        st->codecpar->block_align <= 0) {
+        av_log(s, AV_LOG_ERROR, "Invalid stream parameters:
sample_rate %d, channels %d, block_align %d\n",
+               st->codecpar->sample_rate,
st->codecpar->ch_layout.nb_channels, st->codecpar->block_align);
         return AVERROR_INVALIDDATA;
+    }

     vpk->samples_per_block = (st->codecpar->block_align /
st->codecpar->ch_layout.nb_channels * 28LL) / 16;
     if (vpk->samples_per_block <= 0)
         return AVERROR_INVALIDDATA;
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to