{"record":{"id":"2495b63f8361d73b","repo":"diesel-rs/diesel","slug":"underflow-subtracting-money-amounts","errorCode":null,"errorMessage":"underflow subtracting money amounts","messagePattern":"underflow subtracting money amounts","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel/src/pg/types/money.rs","lineNumber":73,"sourceCode":"    /// Performs a checked addition, and will `panic!` on overflow in both `debug` and `release`.\n    fn add_assign(&mut self, rhs: PgMoney) {\n        self.0 = self\n            .0\n            .checked_add(rhs.0)\n            .expect(\"overflow adding money amounts\")\n    }\n}\n\nimpl Sub for PgMoney {\n    type Output = Self;\n    /// # Panics\n    ///\n    /// Performs a checked subtraction, and will `panic!` on underflow in both `debug` and `release`.\n    fn sub(self, rhs: PgMoney) -> Self::Output {\n        self.0\n            .checked_sub(rhs.0)\n            .map(PgMoney)\n            .expect(\"underflow subtracting money amounts\")\n    }\n}\n\nimpl SubAssign for PgMoney {\n    /// # Panics\n    ///\n    /// Performs a checked subtraction, and will `panic!` on underflow in both `debug` and `release`.\n    fn sub_assign(&mut self, rhs: PgMoney) {\n        self.0 = self\n            .0\n            .checked_sub(rhs.0)\n            .expect(\"underflow subtracting money amounts\")\n    }\n}\n\n#[cfg(feature = \"quickcheck\")]\nmod quickcheck_impls {\n    extern crate quickcheck;","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel/src/pg/types/money.rs#L55-L91","documentation":"Runtime panic from the Sub impl for PgMoney (PostgreSQL money type). Subtraction uses i64::checked_sub on the wrapped cents value and panics on underflow in both debug and release builds instead of wrapping, to avoid silently corrupted monetary results. Fires when `a - b` goes below i64::MIN. Fix: avoid subtracting amounts whose difference underflows i64.","triggerScenarios":"Thrown at diesel/src/pg/types/money.rs:73 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Validate both amounts before subtracting","Use a wider numeric type for intermediate arithmetic","Clamp or reject results that fall below the money range"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}