tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148448583


##########
arrow-buffer/src/bigint.rs:
##########
@@ -243,9 +243,8 @@ impl i256 {
     /// Performs checked addition
     #[inline]
     pub fn checked_add(self, other: Self) -> Option<Self> {
-        let (low, carry) = self.low.overflowing_add(other.low);
-        let high = self.high.checked_add(other.high)?.checked_add(carry as _)?;

Review Comment:
   The mistake this makes is assuming that addition is transitive, in the case 
of overflow it is not. 
   
   In particular consider the case
   
   ```
   i256::from_parts(u128::MAX, i128::MIN)
               .checked_add(i256::from_parts(1, -1))
               .unwrap();
   ```
   
   The carry is enough to increase `i128::MIN` enough that adding `-1` doesn't 
result in overflow



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to