fix: undefined behavior in indexed() for rank-0 histograms#432
Open
henryiii wants to merge 1 commit into
Open
Conversation
Collaborator
|
This needs an update now. I am surprised that a rank 0 histogram still has a single accumulator. I suppose that's consistent with numpy, but not something I remember defining as an explicit edge case. |
henryiii
force-pushed
the
henryiii/fix/rank0-indexed
branch
from
July 23, 2026 17:39
f7585d0 to
45e5004
Compare
For a rank-0 histogram the per-axis index buffer is empty, but operator++ dereferenced its first slot anyway, reading uninitialized memory. Depending on stack contents this walked the cell iterator out of bounds (observed as a segfault on free-threaded Windows in scikit-hep/boost-histogram). Skip the index update when there are no axes; the single cell is still visited once. This also fixes algorithm::sum(h, coverage::inner) and algorithm::empty for rank-0. Fixes boostorg#430 Assisted-by: ClaudeCode:claude-fable-5
henryiii
force-pushed
the
henryiii/fix/rank0-indexed
branch
from
July 23, 2026 17:48
45e5004 to
b95f88b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Fixes #430.
For a rank-0 histogram,
indexed_range::iterator::operator++dereferenced the first slot of the per-axis index buffer, which is empty when there are no axes. That read uninitialized stack memory and, with unlucky stack contents, walked the cell iterator out of bounds — observed as a reproducible segfault on free-threaded Windows via the Python bindings (scikit-hep/boost-histogram#1153).The fix skips the index update when the index buffer is empty; the single cell of a rank-0 histogram is still visited exactly once, so
indexed(),algorithm::sum(h, coverage::inner), andalgorithm::emptynow all work for rank 0.Regression tests cover
indexed()(both coverages),sum, andemptyon a rank-0 histogram. They pass only by luck without the fix (the uninitialized read is stack-layout dependent and needs MemorySanitizer to detect reliably), but they pin down the intended behavior.