Skip to content

deblk: avoid undefined left shift of negative chroma delta#136

Closed
SABITHSAHEB wants to merge 1 commit into
ittiam-systems:mainfrom
SABITHSAHEB:deblk-chroma-delta-shift
Closed

deblk: avoid undefined left shift of negative chroma delta#136
SABITHSAHEB wants to merge 1 commit into
ittiam-systems:mainfrom
SABITHSAHEB:deblk-chroma-delta-shift

Conversation

@SABITHSAHEB

Copy link
Copy Markdown
Contributor

The chroma deblocking filter builds its sample correction from the difference of two neighbouring reconstructed samples and scales it with a left shift, (p - q) << 2. Those samples come straight out of the decoded picture, so the difference is a plain signed int that is negative whenever q is the larger of the two, and a left shift of a negative value is undefined in C. UBSan reports it on a normal decode, several times per frame, while the luma edge filter right beside it already forms the same quantity with multiplication and stays clean. The same shift was copied into every chroma edge routine, including the high bit depth and 4:2:2 builds in ihevc_hbd_deblk_edge_filter.c, so I changed them together. Swapping the shift for a multiply by 4 leaves the result unchanged for the cases that were already well defined and removes the undefined path; a before/after decode of the same stream produces byte-identical YUV. I confirmed the warnings disappear under -fsanitize=undefined using the bundled hevcenc/hevcdec.

@SABITHSAHEB

Copy link
Copy Markdown
Contributor Author

gentle ping

@harishdm

Copy link
Copy Markdown
Member

In general issues of undefined behavior due to shifts are not prioritized.
This works as expected on the platforms where libhevc is currently being used.
Do you have a platform where this causes an issue when running without -fsanitize=undefined?
If not, it is better to stay with the current code.
Same for the other PR too.

@SABITHSAHEB

Copy link
Copy Markdown
Contributor Author

That's fair. I don't have a platform where it actually miscompiles without -fsanitize=undefined. Every compiler these targets use lowers (p - q) << 2 and (p - q) * 4 to the same instructions, so decoded output is identical everywhere today. It surfaced under UBSan on an ordinary decode, so for me it was about keeping the sanitizer baseline clean for fuzzing rather than a runtime bug on your supported platforms.

If clean UBSan output isn't something you're tracking, I'm fine leaving the code as-is. Feel free to close this and #137.

@harishdm

harishdm commented Jul 7, 2026

Copy link
Copy Markdown
Member

Thank you for the response above.
We are not prioritizing UBSan shift and unaligned accesses at this time.

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.

2 participants