Skip to content

fix: avoid quicksort stack overflow on worst-case inputs#6392

Closed
rajatbeladiya wants to merge 1 commit intoOpenZeppelin:masterfrom
rajatbeladiya:fix/arrays-quicksort-recursion-6289
Closed

fix: avoid quicksort stack overflow on worst-case inputs#6392
rajatbeladiya wants to merge 1 commit intoOpenZeppelin:masterfrom
rajatbeladiya:fix/arrays-quicksort-recursion-6289

Conversation

@rajatbeladiya
Copy link

Reworks Arrays._quickSort to recurse on the smaller partition and iterate over the larger partition. This prevents deep recursion on worst-case inputs (e.g., already-sorted/reversed arrays) which can otherwise lead to EVM stack overflow.

Changes:

  • Update generator template scripts/generate/templates/Arrays.js (Arrays.sol is generated).
  • Add a regression test for uint256[] of length 256 (including sorted + reversed cases).

Tested:

  • npm run test:generation
  • npx hardhat test test/utils/Arrays.test.js --grep sort
  • npm run lint:sol

Closes #6289.

Rework Arrays._quickSort to recurse on the smaller partition and iterate over the larger partition, preventing deep recursion on already-sorted/reversed arrays. Add a regression test for uint256 arrays of length 256.
@rajatbeladiya rajatbeladiya requested a review from a team as a code owner March 4, 2026 11:56
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

⚠️ No Changeset found

Latest commit: 36850f8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 214a5bd8-aa05-4df9-be4b-c295f9526a3b

📥 Commits

Reviewing files that changed from the base of the PR and between 95a983a and 36850f8.

📒 Files selected for processing (3)
  • contracts/utils/Arrays.sol
  • scripts/generate/templates/Arrays.js
  • test/utils/Arrays.test.js

Walkthrough

The pull request optimizes the _quickSort() function in the Arrays utility by replacing a two-way recursive quicksort with a hybrid iterative-recursive approach. The implementation wraps the sorting logic in a while loop that continues processing while partition size is at least 0x40. After each partition operation, the code recurses only on the smaller partition and updates the loop bounds to handle the larger partition iteratively. The corresponding template file and tests are updated accordingly, with test coverage expanded to include larger array sizes.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately summarizes the main change: modifying quicksort to avoid stack overflow on worst-case inputs.
Description check ✅ Passed The description is directly related to the changeset, explaining the rework of Arrays._quickSort and the addition of regression tests.
Linked Issues check ✅ Passed The PR implements the primary solution from issue #6289: replacing dual recursion with recursion on smaller partition and iteration on larger partition to prevent stack overflow.
Out of Scope Changes check ✅ Passed All changes are within scope: the template update, generated contract changes, and test additions directly address the stack overflow optimization objective from issue #6289.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Amxx
Copy link
Collaborator

Amxx commented Mar 4, 2026

Duplicate of #6324

@Amxx Amxx marked this as a duplicate of #6324 Mar 4, 2026
@Amxx Amxx closed this Mar 4, 2026
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.

Function _quickSort() can be optimized

2 participants