Repository navigation
Bitstream: Prevent array overreads and undefined behaviour - #198
Conversation
|
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. |
|
@cjee21 please put that one on pause, I am thinking to something else for bitstream handling. |
There was a problem hiding this comment.
🟡 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.
|
Latest test results: https://github.com/cjee21/ZenLib/actions/runs/41829304623/job/126572034842 |
3036c42 to
2e986ed
Compare

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