Skip to content

fix UB when opening an empty sparse layer - #453

Open
youhangwang wants to merge 1 commit into
containerd:mainfrom
youhangwang:fix-ub
Open

youhangwang wants to merge 1 commit into
containerd:mainfrom
youhangwang:fix-ub

Conversation

@youhangwang

@youhangwang youhangwang commented Sep 6, 2026

Copy link
Copy Markdown

What this PR does / why we need it:

open_file_rw() uses &mappings[0] even when mappings may be empty. An empty sparse file produces zero SegmentMapping entries from create_mappings(), making &mappings[0] undefined behavior.

Use mappings.data() instead, which is well-defined for an empty vector and allows the existing zero-length case to be handled safely.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

Signed-off-by: youhwsh <youhangwang@foxmail.com>

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.

🟡 Changes recommended

A small regression test covering “open empty sparse RW layer (0 mappings)” should be added/strengthened to prevent this UB from reappearing unnoticed.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes undefined behavior in the sparse RW open path by avoiding &mappings[0] when the mappings vector is empty, ensuring empty sparse layers can be opened safely.

Changes:

  • Replace &mappings[0] with mappings.data() when constructing the in-memory index for sparse RW layers.
File summaries
File Description
src/overlaybd/lsmt/file.cpp Avoids UB when passing an empty std::vector<SegmentMapping> to create_memory_index0() during sparse RW open.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

LOG_ERROR_RETURN(0, nullptr, "failed to create segment mappings from sparse file!");
}
pi = create_memory_index0((const SegmentMapping *)&mappings[0], mappings.size(),
pi = create_memory_index0(mappings.data(), mappings.size(),
@liulanzheng
liulanzheng requested a review from BigVan September 6, 2026 16:35
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