Skip to content

Fix oracle: validate u128-to-i128 cast before using as price - #488

Merged
IbrahimIjai merged 1 commit into
SO4-Markets:mainfrom
OpenSourceCOntr:fixes/oracle-order-handler-bugs
Jul 24, 2026
Merged

Fix oracle: validate u128-to-i128 cast before using as price#488
IbrahimIjai merged 1 commit into
SO4-Markets:mainfrom
OpenSourceCOntr:fixes/oracle-order-handler-bugs

Conversation

@Uchechukwu-Ekezie

Copy link
Copy Markdown
Contributor

Closes #383

Problem

get_stable_price and get_price_with_stable_fallback both cast a stored u128 directly to i128 without validation. A u128 value at or above 2^127 reinterprets as a negative i128 on this cast; that negative value:

  • Passes the price==0 check (it isn't zero)
  • Is returned unchanged as a legitimate price rather than rejected
  • Downstream code assuming a stable price is always positive would silently misbehave

Fix

  • get_stable_price: Validate raw u128 is below i128::MAX before casting. Reject if zero or would cast to negative, return None
  • get_price_with_stable_fallback: Only use stable price if it's positive AND fits in i128. Otherwise fall back to primary price

This matches the validation applied to keeper-submitted prices which explicitly reject non-positive prices at write time.

Testing

  • Oracle contract compiles without errors
  • Validation logic prevents negative prices from being accepted
  • Fallback to primary price when stable price is invalid

Closes SO4-Markets#383

Issue: get_stable_price and get_price_with_stable_fallback both cast a
stored u128 directly to i128 without validation. A u128 value at or above
2^127 reinterprets as a negative i128 on this cast; that negative value
passes the price==0 check (it isn't zero) and is returned unchanged as a
legitimate price rather than rejected.

Fix:
- get_stable_price: Validate raw u128 is below i128::MAX before casting
  and reject if zero or would cast to negative
- get_price_with_stable_fallback: Only use stable price if it's positive
  and fits in i128, otherwise fall back to primary price

This matches the validation applied to keeper-submitted prices which
explicitly reject non-positive prices at write time.
@drips-wave

drips-wave Bot commented Jul 24, 2026

Copy link
Copy Markdown

@Uchechukwu-Ekezie Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@IbrahimIjai

Copy link
Copy Markdown
Contributor

GReat...

@IbrahimIjai
IbrahimIjai merged commit a78156d into SO4-Markets:main Jul 24, 2026
0 of 2 checks passed
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.

oracle: get_stable_price / get_price_with_stable_fallback trust an unchecked u128->i128 cast as a valid price

2 participants