avcodec/h264_slice: guard color_frame() against chroma-width underflow
authorFranciszek Kalinowski <franek.kalinowski@isec.pl>
Tue, 19 May 2026 07:36:50 +0000 (09:36 +0200)
committerMichael Niedermayer <michael@niedermayer.cc>
Thu, 18 Jun 2026 18:30:55 +0000 (20:30 +0200)
commit7926bdc77560f2d7c8d78af22eca7fd14da56c77
treeea07c8e3cd505ffb0e4a7ab2a6af0176c3017856
parenta49c00c20896b3c4c6efbf8c31d55f22d10168da
avcodec/h264_slice: guard color_frame() against chroma-width underflow

In the >= 9 bit path, color_frame() does
`av_memcpy_backptr(dst + 2, 2, bytes - 2)`. When the effective chroma width
is 1 pixel (bytes == 1) the count becomes -1 and the underlying fill16()
loop runs roughly 2^32 times, producing a heap overflow. The original count
was also wrong in units (pixels rather than bytes); fix that at the same
time so the 2-pixel case still fills both pixels.

Confirmed via a standalone harness reproducing av_memcpy_backptr's fill16
loop with cnt = -1; reaching the call from a crafted H.264 bitstream
requires Hi10P plus a frame_num gap on a frame whose effective chroma width
is 1 pixel, which is hard to express but is reachable via mid-stream SPS
changes. Compiles cleanly; no regressions seen running existing crafted
H.264 PoCs and trivial transcodes.

Reported by Franciszek Kalinowski (isec.pl / striga.ai) and Bartosz Smigielski.

(cherry picked from commit c79dfd29e6afca56294395be843d98d10858a7fa)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
libavcodec/h264_slice.c