Fix panic when sorting prices containing NaN in test#94
Closed
aviu16 wants to merge 1 commit intoflowsurface-rs:mainfrom
Closed
Fix panic when sorting prices containing NaN in test#94aviu16 wants to merge 1 commit intoflowsurface-rs:mainfrom
aviu16 wants to merge 1 commit intoflowsurface-rs:mainfrom
Conversation
The smallest_positive_gap test helper panics when the input prices contain NaN values, because partial_cmp returns None for NaN comparisons. Changed .unwrap() to .unwrap_or(Ordering::Equal) so NaN values are treated as equal during sorting, preventing the panic. While this is currently only used in test code, using unwrap_or makes the code more defensive against unexpected NaN inputs.
1d15f51 to
dc7cced
Compare
Author
|
closing this for now, gonna revisit later |
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.
Summary
Fixes a potential panic in the
smallest_positive_gaptest helper function when input prices contain NaN values.Bug Description
In
exchange/src/adapter/hyperliquid.rs, thesmallest_positive_gapfunction panics when sorting prices that contain NaN:The
partial_cmpmethod returnsNonewhen comparing NaN values (per IEEE 754 floating point spec), causing.unwrap()to panic.Root Cause
Using
.unwrap()onpartial_cmpwithout handling the NaN case.Fix
Changed to
.unwrap_or(std::cmp::Ordering::Equal)to treat NaN values as equal during sorting:This prevents the panic while maintaining correct sort behavior for valid numbers.
Impact
#[cfg(test)]module)Testing
The fix ensures that if test data ever contains NaN values (e.g., from invalid price feeds or calculation errors), the test helper won't panic.