Skip to content

decoder: Do not keep an output stride narrower than the picture - #41

Merged
harishdm merged 1 commit into
ittiam-systems:mainfrom
GeekQAQ:stride-not-below-width
Nov 20, 2026
Merged

harishdm merged 1 commit into
ittiam-systems:mainfrom
GeekQAQ:stride-not-below-width

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

u4_frm_buf_stride is set to the picture width after the header only when it is 0. It can be smaller than the width at that point:

  • a decode call returns IVD_RES_CHANGED (without the fatal bit) and the application keeps feeding data without a reset: the stride of the previous, narrower sequence is kept for the new one;
  • a display width set with IVD_CMD_CTL_SETPARAMS before the first header is accepted without being checked against the picture width.

The 420SP (UV and VU) and 422ILE format conversions advance the destination pointer at the end of each row by the unsigned difference stride - width, so with a stride below the width they write outside the output buffer (AddressSanitizer: SEGV on a write in impeg2_fmt_conv_yuv420p_to_yuv420sp_uv, _yuv420sp_vu and _yuv422ile). With 420P output the rows overlap instead. The example and the fuzzer reset on IVD_RES_CHANGED, so they do not take the first path.

This change uses the picture width when the stride is below it.

Tested on main (275c536) with clang 21 and AddressSanitizer: both cases now decode to the end with the stride equal to the picture width, for all four output formats (before: SEGV in the three conversions above). The program and the streams are in this gist: https://gist.github.com/GeekQAQ/4c0803d0b94d7c65958cbcba726febc6 The default CMake build (Release, gcc and clang) has no new warnings, and mpeg2dec gives byte-identical output before and after the change on 16 short test streams with 1 core, and with 4 cores except for a stream that starts with a B picture, whose 4-core output already varies from run to run on main (a separate issue, sent as its own PR).

u4_frm_buf_stride is set to the picture width after the header only when
it is 0. It can still be smaller than the width there:

- after a decode call returns IVD_RES_CHANGED (not a fatal error) and the
  application keeps feeding data without a reset, the stride of the
  previous, narrower sequence is kept for the new one;
- a display width set with IVD_CMD_CTL_SETPARAMS before the first
  header is accepted without checking it against the picture width.

The 420SP (UV and VU) and 422ILE format conversions advance the
destination pointer at the end of each row by the unsigned difference
"stride - width", so with a stride below the width they write
outside the output buffer. With 420P output the rows overlap instead.

Use the picture width when the stride is below it.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@harishdm

Copy link
Copy Markdown
Member

After IVD_RES_CHANGED, application needs to flush all the buffers and then call reset and then start feeding the data.

If this is not done, the decoder output is not guaranteed to be as expected.

Given this, can we skip this?

@GeekQAQ

GeekQAQ commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for merging the other four PRs.

For this one, the effect is not just unexpected output: the format conversion writes outside the output buffer the application passed in. On main (0e733da, with #37–#40) the reproducer linked in the description still ends in an AddressSanitizer SEGV on a WRITE in impeg2_fmt_conv_yuv420p_to_yuv420sp_uv (impeg2_format_conv.c:390), impeg2_fmt_conv_yuv420p_to_yuv420sp_vu (:332) and impeg2_fmt_conv_yuv420p_to_yuv422ile (:239), because the unsigned row step stride - width wraps when the stride is below the width. The same write also happens without IVD_RES_CHANGED, when a display width smaller than the picture width is set with IVD_CMD_CTL_SETPARAMS before the first header (that call returns success). As you mentioned in #42 that the decoder ensures there are no OOB reads or writes, I think this case falls under that guarantee.

The change only keeps the stride from going below the width. It does not change the requirement that the application flushes and resets after IVD_RES_CHANGED.

I'll leave it to you whether to take it.

@harishdm

Copy link
Copy Markdown
Member

Thank you for the explaination.

@harishdm
harishdm merged commit 7c88238 into ittiam-systems:main Nov 20, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants