From e3eb633ac908e52865eb761cbf46ed5672fb6cbf Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sat, 28 Nov 2015 00:42:35 -0500 Subject: [PATCH] cut: fix off-by-one error for range calculation When determining the range from which to select portions of a line, the upper limit of the range is a usize. The maximum upper value is usize::MAX, but at one point this value is incremented, causing an overflow. By setting the maximum upper value to usize::MAX-1, the bug is averted. Since the upper limit of the range is an index (thus, ranging from 0 to 2^64-1 for 64-bit platforms), the maximum usize should not be reached. --- src/cut/ranges.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cut/ranges.rs b/src/cut/ranges.rs index 3e5bfd3d5..a25d0da90 100644 --- a/src/cut/ranges.rs +++ b/src/cut/ranges.rs @@ -37,7 +37,7 @@ impl FromStr for Range { } (Some(n), Some(m)) if m.len() == 0 => { if let Ok(low) = n.parse::() { - if low > 0 { Ok(Range{ low: low, high: MAX}) } else { Err(field) } + if low > 0 { Ok(Range{ low: low, high: MAX - 1}) } else { Err(field) } } else { Err(inval) } @@ -118,10 +118,10 @@ pub fn complement(ranges: &Vec) -> Vec { } } (Some(last), None) => { - if last.high < usize::MAX { + if last.high < usize::MAX - 1 { complements.push(Range { low: last.high + 1, - high: usize::MAX + high: usize::MAX - 1 }); } }