Skip to content

BUG: Copy the shuffled order when assigning a RandomPermutation - #7068

Open
hjmjohnson wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-random-permutation-copy
Open

hjmjohnson wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-random-permutation-copy

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

RandomPermutation::operator= reallocates its node array without copying the nodes. A copy-assigned ImageRandomNonRepeatingConstIteratorWithIndex therefore 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 main independently of that PR, so the fix is kept separate to keep #7065 small.

What changed
  • RandomPermutation gets 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.
  • The iterator's operator= copies m_NumberOfSamplesRequested, m_NumberOfSamplesDone, and m_NumberOfPixelsInRegion with 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::SetPriority rejects an index equal to the size (the check was >).
  • The iterator gets a copy constructor. The implicit one copied the m_Permutation pointer, 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.RandomNonRepeatingCopyAssignmentKeepsVisitOrder compares visit orders after assigning into an iterator over the same region, a default-constructed one, and one over a smaller region with one sample. On main these visit {0, 0} sixteen times, nothing (arm64; x86 traps on a division by zero), and one pixel.

ImageIterators.RandomPermutationCopyIsIndependent checks that a copied and an assigned permutation keep the order, that reseeding them leaves the original's next shuffle unchanged, and the SetPriority bound.

ImageIterators.RandomNonRepeatingCopyOutlivesSource copies an iterator, destroys the source, and checks that the copy still visits every pixel once; on main it crashes.

All three fail on the unfixed headers. The 14 iterator tests in ITKCommon pass on macOS arm64 (clang 19, Release).

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module labels Nov 1, 2026
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 1, 2026 19:55
@hjmjohnson
hjmjohnson requested a review from N-Dekker November 1, 2026 19:56
@greptile-apps

greptile-apps Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The changes appear safe to merge.

Summary

This PR makes copies of RandomPermutation and its iterator own separate permutations.

  • Copied random iterators keep their own shuffled visit order.
  • Copied permutations keep their order without sharing their random generator.
  • Priority ranks reject the first index past the permutation.

No new actionable issues were found in the changes since the previous review. hjmjohnson explicitly deferred the known both-null assignment and converting-constructor issue to an iterator follow-up.

Reviews (5) · Last reviewed commit: "BUG: Deep copy the permutation when copy..." · Reviewed by Greptile

@hjmjohnson
hjmjohnson requested a review from dzenanz November 5, 2026 23:59
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push 0fd7e8e..246da27 is a plain rebase on main, with no content change. It resolves a conflict with #7069, which appended a test at the same place in itkImageIteratorsGTest.cxx; both tests are kept.

@hjmjohnson
hjmjohnson force-pushed the bug-random-permutation-copy branch from 0fd7e8e to 246da27 Compare November 6, 2026 00:02
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

@hjmjohnson
hjmjohnson force-pushed the bug-random-permutation-copy branch from 246da27 to c32c56a Compare November 6, 2026 01:51
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push 246da27..c32c56a is content only, on the same base: copy-and-swap assignment and a copy constructor for RandomPermutation, an independent generator per copy, the iterator's sample counters copied on assignment, and the SetPriority bound. The body lists the details.

@hjmjohnson
hjmjohnson force-pushed the bug-random-permutation-copy branch from c32c56a to d43b901 Compare November 6, 2026 01:55
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.
@hjmjohnson
hjmjohnson force-pushed the bug-random-permutation-copy branch from d43b901 to 33cdb68 Compare November 6, 2026 03:28
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push d43b901..33cdb68 is content only, on the same base: size in the new copy test is now static constexpr, because MSVC rejected reading it inside the lambda without a capture (Pixi-Cxx windows-2022, C3493).

@hjmjohnson

Copy link
Copy Markdown
Member Author

@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.
@hjmjohnson
hjmjohnson force-pushed the bug-random-permutation-copy branch from ccf7dd8 to 4fefb44 Compare November 9, 2026 17:12
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push ccf7dd83b3e..4fefb44b5b1 is content only, with the same base. The copy constructor now copies the source permutation directly (new RandomPermutation(*it.m_Permutation)), as operator= does, so it no longer builds a throwaway permutation and cannot leak one if the copy throws. Raised by Greptile on #7135, which stacks on this PR.

@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant