Skip to content

Bitstream: Prevent array overreads and undefined behaviour - #198

Merged
JeromeMartinez merged 4 commits into
MediaArea:masterfrom
cjee21:safety
Nov 7, 2026
Merged

JeromeMartinez merged 4 commits into
MediaArea:masterfrom
cjee21:safety

Conversation

@cjee21

@cjee21 cjee21 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Attempt to fix #196 as well as reduce MediaInfoLib crashes.

@cjee21

cjee21 commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Test results comparing master branch and this PR: https://github.com/cjee21/ZenLib/actions/runs/26945178715/job/79496251264

The tests are mostly written with AI. Let me know if you want to integrate the tests here.

@JeromeMartinez

Copy link
Copy Markdown
Member

@cjee21 please put that one on pause, I am thinking to something else for bitstream handling.

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.

🟡 Changes recommended

An oversized skip continues looping after an overrun detaches the buffer, causing roughly SIZE_MAX / 32 iterations and effectively hanging the caller on 64-bit platforms.

1 open issue
What changed in this PR

This PR hardens ZenLib’s bitstream readers against invalid bit counts and addresses the large-skip issue described in #196, aiming to reduce MediaInfoLib crashes.

Changes:

  • Add bit-count guards to little-endian reads and fast read/peek methods.
  • Split large little-endian skips into chunks of at most 32 bits.
  • Correct little-endian byte alignment to consume the remaining bits in the current byte.
File Description
Source/​ZenLib/​BitStream_LE.h Guards read widths, chunks large skips, and corrects byte alignment.
Source/​ZenLib/​BitStream_Fast.h Guards bit counts in 8-, 16-, and 32-bit read/peek methods.

Review effort: Balanced

if (bits>32) //Get is only for <=32 bits
{
do {
Get(32);
Return immediately when a 32-bit skip or read fails in either bitstream implementation, preventing further processing after the buffer is exhausted.
@cjee21

cjee21 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@JeromeMartinez
JeromeMartinez merged commit 5ea38de into MediaArea:master Nov 7, 2026
16 checks passed
@cjee21
cjee21 deleted the safety branch November 7, 2026 16:57
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.

Vulnerability in BitStream_LE::Skip(size_t bits)

3 participants