Repository navigation
BUG: Copy the shuffled order when assigning a RandomPermutation - #7068
hjmjohnson wants to merge 2 commits into
Conversation
[Medium impact] The changes appear safe to merge.
|
0fd7e8e to
246da27
Compare
|
@greptileai review |
246da27 to
c32c56a
Compare
c32c56a to
d43b901
Compare
RandomPermutation::operator= reallocated its node array but never copied the nodes into it, so every node kept index zero. Copy-assigning an ImageRandomNonRepeatingConstIteratorWithIndex therefore produced an iterator that visited the region's first pixel for every sample. Copy the nodes through a new copy constructor, which also gives the copy its own generator, seeded with the source's seed: the implicit one shared the node array, and sharing the generator let reseeding one permutation change the other's order. The iterator's operator= now copies the sample counters too, so a target that was default-constructed or over another region visits the source's samples. SetPriority rejects an index equal to the size.
d43b901 to
33cdb68
Compare
|
@greptileai review |
…erator The implicit copy constructor of ImageRandomNonRepeatingConstIteratorWithIndex copied the m_Permutation pointer, so the copy and the source shared one permutation and each deleted it. A copy used after the source was destroyed read freed memory. The copy constructor now copies the permutation and the sample counters, as operator= does.
ccf7dd8 to
4fefb44
Compare
|
Force-push |
|
@greptileai review |
RandomPermutation::operator=reallocates its node array without copying the nodes. A copy-assignedImageRandomNonRepeatingConstIteratorWithIndextherefore visits the region's first pixel for every sample. This copies the nodes, makes copies of a permutation independent, copies the iterator's sample counters on assignment, and gives the iterator a copy constructor that deep copies the permutation.Found while triaging #7065, whose new read-only conversion also goes through this assignment. The bug is on
mainindependently of that PR, so the fix is kept separate to keep #7065 small.What changed
RandomPermutationgets a copy constructor. The implicit one shared the node array, so a copy double-deleted it. The copy also gets its own generator, started from the source's seed (not its current state), so reseeding one permutation no longer changes the other's order.RandomPermutation::operator=is copy-and-swap, so a failed allocation leaves the target unchanged.operator=copiesm_NumberOfSamplesRequested,m_NumberOfSamplesDone, andm_NumberOfPixelsInRegionwith the permutation. Before, a target that was default-constructed divided by a zero pixel count (nothing visited on arm64, SIGFPE on x86), and one over a smaller region kept its own sample count.RandomPermutation::SetPriorityrejects an index equal to the size (the check was>).m_Permutationpointer, so the copy and the source each deleted the same permutation, and a copy used after its source was destroyed read freed memory (second commit).Tests
ImageIterators.RandomNonRepeatingCopyAssignmentKeepsVisitOrdercompares visit orders after assigning into an iterator over the same region, a default-constructed one, and one over a smaller region with one sample. Onmainthese visit{0, 0}sixteen times, nothing (arm64; x86 traps on a division by zero), and one pixel.ImageIterators.RandomPermutationCopyIsIndependentchecks that a copied and an assigned permutation keep the order, that reseeding them leaves the original's next shuffle unchanged, and theSetPrioritybound.ImageIterators.RandomNonRepeatingCopyOutlivesSourcecopies an iterator, destroys the source, and checks that the copy still visits every pixel once; onmainit crashes.All three fail on the unfixed headers. The 14 iterator tests in
ITKCommonpass on macOS arm64 (clang 19, Release).