Skip to content

Fix MSan use-of-uninitialized-value in LibArchiveWriter - #97431

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix-msan-libarchive-writer-uninitialized
Feb 22, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix-msan-libarchive-writer-uninitialized

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 19, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix MSan use-of-uninitialized-value in LibArchiveWriter::WriteBufferFromLibArchive::closeFile by initializing all class members at declaration
  • Add a gtest (EmptyFileWithKnownSize) that reproduces the exact MSan error — verified it fails without the fix and passes with it

The bug was triggered by BuzzHouse running incremental BACKUP queries to tar archives. When a backup entry announced a file size via writeFile(filename, size) but no data was actually written (0 bytes copied), writeEntry was never called, leaving expected_size uninitialized. Then closeFile(true) read it.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=652baa1545bf0933e7a0728edac219ddfe9738f5&name_0=MasterCI&name_1=BuzzHouse%20%28amd_msan%29&name_1=BuzzHouse%20%28amd_msan%29

Closes #97492

Test plan

  • Verified under MSan: test fails without fix, passes with fix
  • All existing archive tests pass under MSan

Changelog category

CI Fix or improvement

Changelog entry

Fix MSan use-of-uninitialized-value in LibArchiveWriter::WriteBufferFromLibArchive::closeFile by initializing class members at declaration.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.1035

alexey-milovidov and others added 2 commits February 19, 2026 23:02
Fix MSan use-of-uninitialized-value in `LibArchiveWriter::WriteBufferFromLibArchive::closeFile`.

The `expected_size` member was not initialized, so if `writeEntry` was
never called (e.g. no data written to the buffer), reading `expected_size`
in `closeFile` at line 198 triggered the MSan warning.

Initialize all non-const non-class-type members at declaration.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=652baa1545bf0933e7a0728edac219ddfe9738f5&name_0=MasterCI&name_1=BuzzHouse%20%28amd_msan%29&name_1=BuzzHouse%20%28amd_msan%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…chive::closeFile`

The test creates a tar archive file entry with known size=0, writes no data,
and finalizes the buffer. This exercises the code path where `expected_size`
was previously uninitialized, reproducing the MSan error from BuzzHouse CI.

Verified that:
- Without the fix, MSan catches the exact same error as CI
- With the fix, the test passes cleanly under MSan

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a6a4313]

Summary: ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a Memory Sanitizer (MSan) use-of-uninitialized-value bug in LibArchiveWriter::WriteBufferFromLibArchive::closeFile(). The bug occurred when a backup entry declared a file size via writeFile(filename, size) with size=0, but no data was actually written. In this scenario, writeEntry() was never called (it's only invoked when data is written), leaving expected_size uninitialized. When closeFile(true) was later called, it would read the uninitialized value at line 198.

Changes:

  • Initialize all class members (entry, archive, size, expected_size) at declaration in WriteBufferFromLibArchive
  • Add a gtest that reproduces the exact MSan error scenario with a 0-byte file

Reviewed changes

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

File Description
src/IO/Archives/LibArchiveWriter.cpp Initialize member variables at declaration to fix MSan uninitialized value bug
src/IO/tests/gtest_archive_reader_and_writer.cpp Add test case for empty file with known size to verify the fix

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I added an extra commit to add an extra check: 6fe0e8c

Entry entry = nullptr;
Archive archive = nullptr;
size_t size = 0;
size_t expected_size = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the actual fix, but initializing the rest makes sense as a defensive programming measure.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Feb 22, 2026
Merged via the queue into master with commit 5cf4809 Feb 22, 2026
144 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-msan-libarchive-writer-uninitialized branch February 22, 2026 15:26
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemorySanitizer: use-of-uninitialized-value (STID: 5536-4cc0)

4 participants