Skip to content

Price struct integer overflow #228

Open

Description

@cuongph87

What version are you using?

Version 22.0.0. But it shouldn't matter.

What did you do?

While the XDR definition specifies int32, the actual Stellar network allows for prices with numerators and denominators exceeding the int32 range. This causes parsing failures when attempting to process such transactions.

A real-world example can be found in this trade:
https://horizon.stellar.org/trades?cursor=237504432974213121-0&limit=1

What did you expect to see?

Larger values observed in the network should be handled properly.

Activity

  1. leocagli commented on Jun 26, 2026

    @leocagli

    Hi, I'd like to investigate and fix the integer overflow in the Price struct. I can add checked arithmetic and a test with boundary values. Could you assign me?

  2. leocagli commented on Jun 27, 2026

    @leocagli

    Hi, Leo Cagliero (Open Stellar / Starmaker). I'd like to take this.

    Price { n: i32, d: i32 } overflows when comparing or cross-multiplying two prices: a.n * b.d and b.n * a.d are computed in i32, and with large numerators/denominators the product exceeds i32::MAX. The fix is to widen to i64 (or i128) for the intermediate products before comparing:

    impl Price {
        pub fn cmp_cross(&self, other: &Price) -> Ordering {
            // a.n/a.d  vs  b.n/b.d   ==>   a.n*b.d  vs  b.n*a.d   (widened)
            let lhs = i64::from(self.n) * i64::from(other.d);
            let rhs = i64::from(other.n) * i64::from(self.d);
            lhs.cmp(&rhs)
        }
    }

    i64::from(i32) * i64::from(i32) can never overflow i64, so the comparison stays exact. If there's a place that materializes the product into XDR (not just compares), I'll use checked_mul there and surface an error instead of wrapping. I'll add tests with n = i32::MAX, d = 1 against another large price to confirm no overflow and correct ordering. Could you assign me?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions