Bounds check Solver::swap return in trade verifier#4432
Merged
Conversation
Solver::swap return in trade verifier
jmg-duarte
approved these changes
May 22, 2026
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a length validation check for queriedBalances in SettleOutput::from_swap to prevent out-of-bounds panics when the solver returns an unexpected number of balances. Regression and success unit tests have been added to ensure correct error handling and normal operation. No critical issues found; I have no feedback to provide.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
TradeVerifier::SettleOutput::from_swapindexes thequeriedBalancesarray returned from the on-chainSolverhelper without checking its length. The function expects2 * tokens.len() + 2entries laid out as[...tokens_before, user_before, user_after, ...tokens_after]. When the helper returns fewer entries the indexing panics withindex out of bounds: the len is 4 but the index is 4, killing the orderbook tokio worker.The panic was observed on barn while submitting a wrapper-bearing order:
POST /api/v1/ordersre-runs the quoter viaget_quote_and_check_fee, the verified estimator drivesTradeVerifier::verify_innerand crashes insidefrom_swap. The container restart loop surfaces as 502s and looks like OOM in metrics.A separate investigation is needed for why the helper returns a short response for wrapper-bearing orders (Bug B). This PR addresses only the panic so the verifier fails soft.
Changes
queriedBalances.len()against the expected2 * tokens.len() + 2at the top ofSettleOutput::from_swapand return a descriptiveanyhowerror if the shape is unexpected. Callers up the stack already surface this asError::SimulationFailed(unverified quote) instead of panicking.How to test
New unit tests cover both the error path (short response) and the happy path.