{"record":{"id":"ea0e5716f3af3d0a","repo":"diesel-rs/diesel","slug":"overflow-adding-money-amounts","errorCode":null,"errorMessage":"overflow adding money amounts","messagePattern":"overflow adding money amounts","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel/src/pg/types/money.rs","lineNumber":48,"sourceCode":"}\n\n#[cfg(feature = \"postgres_backend\")]\nimpl ToSql<Money, Pg> for PgMoney {\n    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {\n        ToSql::<BigInt, Pg>::to_sql(&self.0, out)\n    }\n}\n\nimpl Add for PgMoney {\n    type Output = Self;\n    /// # Panics\n    ///\n    /// Performs a checked addition, and will `panic!` on overflow in both `debug` and `release`.\n    fn add(self, rhs: PgMoney) -> Self::Output {\n        self.0\n            .checked_add(rhs.0)\n            .map(PgMoney)\n            .expect(\"overflow adding money amounts\")\n    }\n}\n\nimpl AddAssign for PgMoney {\n    /// # Panics\n    ///\n    /// 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","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel/src/pg/types/money.rs#L30-L66","documentation":"Runtime panic from the Add impl for PgMoney (PostgreSQL money type). PgMoney wraps an i64 cents value; addition uses i64::checked_add and deliberately panics on overflow in both debug and release builds rather than wrapping around, because a wrapped monetary amount would be silently wrong. Fires when `a + b` exceeds i64::MAX. Fix: use smaller amounts or handle the sum with checked arithmetic before adding.","triggerScenarios":"Thrown at diesel/src/pg/types/money.rs:48 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Validate both amounts before adding","Use a wider numeric type for intermediate arithmetic","Clamp or reject amounts that exceed 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"}