Skip to content

Eliminated the possibility for zim::Compressor being trapped in an infinite loop#1047

Merged
kelson42 merged 2 commits intomainfrom
compression_infinite_loop
Mar 15, 2026
Merged

Eliminated the possibility for zim::Compressor being trapped in an infinite loop#1047
kelson42 merged 2 commits intomainfrom
compression_infinite_loop

Conversation

@veloman-yunkan
Copy link
Copy Markdown
Collaborator

@veloman-yunkan veloman-yunkan commented Mar 13, 2026

Fixes #1046

Added a small unit-test demonstrating the bug and fixed it.

Also checked if zim::Uncompressor suffers from a similar bug but to the best of my understanding it is devoid of such defects.

Added a test case demonstrating how an infinite loop can be forced
onto zim::Compressor.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.26%. Comparing base (f8cc2cb) to head (dae246c).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/compression.h 75.00% 0 Missing and 3 partials ⚠️

❌ Your patch status has failed because the patch coverage (75.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1047   +/-   ##
=======================================
  Coverage   56.25%   56.26%           
=======================================
  Files         101      101           
  Lines        5016     5014    -2     
  Branches     2185     2186    +1     
=======================================
- Hits         2822     2821    -1     
+ Misses        738      737    -1     
  Partials     1456     1456           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a reported freeze in libzim where zim::Compressor could get stuck in an infinite loop during Zstd streaming compression when the output buffer becomes exactly full.

Changes:

  • Adjusts zim::Compressor’s encoding loop to grow the output buffer proactively when avail_out == 0, preventing a non-progressing loop.
  • Makes Compressor’s constructor explicit and adds an invariant check for initial_size > 0.
  • Adds a regression unit test intended to reproduce the “buffer exactly filled” scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/compression.cpp Adds a Zstd-focused regression test for the “output buffer exactly full” case.
src/compression.h Updates Compressor’s constructor and feed() loop to avoid infinite loops when the output buffer is exhausted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

compressor.init(&data[0]);
compressor.feed(&data[0], DATA_SIZE);
zim::zsize_t compressedDataSize2;
const auto out2 = compressor.get_data(&compressedDataSize2);
Comment on lines +230 to +235
explicit Compressor(size_t initial_size=1024*1024) :
ret_data(new char[initial_size]),
ret_size(initial_size)
{}
{
ASSERT(initial_size, >, 0u);
}
Copy link
Copy Markdown

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

I barely understand C++ code, but it seems to make sense to avoid the bug.

@kelson42
Copy link
Copy Markdown
Contributor

kelson42 commented Mar 15, 2026

Then, I guess we are good to merge!
@veloman-yunkan Thank you so much for the quick fix
I will do a patch release within a few days

@kelson42 kelson42 merged commit efc4e2a into main Mar 15, 2026
34 of 35 checks passed
@kelson42 kelson42 deleted the compression_infinite_loop branch March 15, 2026 08:29
@benoit74
Copy link
Copy Markdown

Yup, thank you very much!

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.

Libzim suddenly "freeze" while adding a specific items sequence

4 participants