This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-bytes-range-u64-max-bounds in repository https://gitbox.apache.org/repos/asf/opendal.git
commit c63ba938640a02520b616fe7f3339b39ea6d4031 Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 18:18:34 2026 +0100 fix(core): handle u64::MAX bounds in BytesRange::from Compute inclusive range sizes via (end - offset).saturating_add(1) instead of saturating end + 1 before subtracting offset, fixing off-by-one sizes for 1..=u64::MAX and empty sizes for u64::MAX..=u64::MAX and matching FromStr. Return an empty BytesRange::new(u64::MAX, Some(0)) when start_bound is Bound::Excluded(u64::MAX) instead of saturating the start offset back to u64::MAX. --- core/core/src/types/bytes_range.rs | 68 ++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/core/core/src/types/bytes_range.rs b/core/core/src/types/bytes_range.rs index 958ac97548..7c756f4f9b 100644 --- a/core/core/src/types/bytes_range.rs +++ b/core/core/src/types/bytes_range.rs @@ -334,11 +334,18 @@ where fn from(range: T) -> Self { let offset = match range.start_bound().cloned() { Bound::Included(n) => n, - Bound::Excluded(n) => n.saturating_add(1), + Bound::Excluded(n) => match n.checked_add(1) { + Some(offset) => offset, + None => return BytesRange::new(u64::MAX, Some(0)), + }, Bound::Unbounded => 0, }; let size = match range.end_bound().cloned() { - Bound::Included(n) => Some(n.saturating_add(1).saturating_sub(offset)), + Bound::Included(n) => Some(if n < offset { + 0 + } else { + (n - offset).saturating_add(1) + }), Bound::Excluded(n) => Some(n.saturating_sub(offset)), Bound::Unbounded => None, }; @@ -479,28 +486,77 @@ mod tests { assert_eq!(BytesRange::new(10, Some(0)), BytesRange::from(10..=5)); assert_eq!(BytesRange::new(5, Some(0)), BytesRange::from(5..0)); assert_eq!(BytesRange::new(5, Some(0)), BytesRange::from(5..=0)); + assert_eq!( + BytesRange::new(u64::MAX, Some(0)), + BytesRange::from(u64::MAX..=0) + ); } #[test] - fn test_bytes_range_from_range_bounds_u64_max() { - // Boundary cases near u64::MAX must not overflow. + fn test_bytes_range_from_range_bounds_u64_max() -> Result<()> { + // Boundary cases near u64::MAX must not overflow or drop a byte. assert_eq!( BytesRange::new(0, Some(u64::MAX)), BytesRange::from(..=u64::MAX) ); + assert_eq!( + BytesRange::new(0, Some(u64::MAX)), + BytesRange::from(0..=u64::MAX) + ); assert_eq!( BytesRange::new(0, Some(u64::MAX)), BytesRange::from(..u64::MAX) ); assert_eq!( - BytesRange::new(1, Some(u64::MAX.saturating_sub(1))), + BytesRange::new(1, Some(u64::MAX)), BytesRange::from(1..=u64::MAX) ); - // Excluded start at u64::MAX must not overflow. + assert_eq!( + BytesRange::new(u64::MAX, Some(1)), + BytesRange::from(u64::MAX..=u64::MAX) + ); assert_eq!( BytesRange::new(u64::MAX, None), BytesRange::from((u64::MAX)..) ); + + // Parity with FromStr at u64::MAX boundaries. + assert_eq!( + BytesRange::from(0..=u64::MAX), + "bytes=0-18446744073709551615".parse::<BytesRange>()? + ); + assert_eq!( + BytesRange::from(1..=u64::MAX), + "bytes=1-18446744073709551615".parse::<BytesRange>()? + ); + assert_eq!( + BytesRange::from(u64::MAX..=u64::MAX), + "bytes=18446744073709551615-18446744073709551615".parse::<BytesRange>()? + ); + + // Excluded start near and at u64::MAX. + assert_eq!( + BytesRange::new(u64::MAX, Some(1)), + BytesRange::from((Bound::Excluded(u64::MAX - 1), Bound::Included(u64::MAX))) + ); + assert_eq!( + BytesRange::new(u64::MAX, None), + BytesRange::from((Bound::Excluded(u64::MAX - 1), Bound::Unbounded)) + ); + assert_eq!( + BytesRange::new(u64::MAX, Some(0)), + BytesRange::from((Bound::Excluded(u64::MAX), Bound::Unbounded)) + ); + assert_eq!( + BytesRange::new(u64::MAX, Some(0)), + BytesRange::from((Bound::Excluded(u64::MAX), Bound::Included(u64::MAX))) + ); + assert_eq!( + BytesRange::new(u64::MAX, Some(0)), + BytesRange::from((Bound::Excluded(u64::MAX), Bound::Excluded(u64::MAX))) + ); + + Ok(()) } #[test]
