@@ -200,18 +200,20 @@ impl<T> Cursor<T> {
200
200
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
201
201
impl < T > io:: Seek for Cursor < T > where T : AsRef < [ u8 ] > {
202
202
fn seek ( & mut self , style : SeekFrom ) -> io:: Result < u64 > {
203
- let pos = match style {
204
- SeekFrom :: Start ( n) => { self . pos = n; return Ok ( n) }
205
- SeekFrom :: End ( n) => self . inner . as_ref ( ) . len ( ) as i64 + n ,
206
- SeekFrom :: Current ( n) => self . pos as i64 + n ,
203
+ let ( base_pos , offset ) = match style {
204
+ SeekFrom :: Start ( n) => { self . pos = n; return Ok ( n) ; }
205
+ SeekFrom :: End ( n) => ( self . inner . as_ref ( ) . len ( ) as u64 , n ) ,
206
+ SeekFrom :: Current ( n) => ( self . pos , n ) ,
207
207
} ;
208
-
209
- if pos < 0 {
210
- Err ( Error :: new ( ErrorKind :: InvalidInput ,
211
- "invalid seek to a negative position" ) )
208
+ let new_pos = if offset >= 0 {
209
+ base_pos. checked_add ( offset as u64 )
212
210
} else {
213
- self . pos = pos as u64 ;
214
- Ok ( self . pos )
211
+ base_pos. checked_sub ( ( offset. wrapping_neg ( ) ) as u64 )
212
+ } ;
213
+ match new_pos {
214
+ Some ( n) => { self . pos = n; Ok ( self . pos ) }
215
+ None => Err ( Error :: new ( ErrorKind :: InvalidInput ,
216
+ "invalid seek to a negative or overflowing position" ) )
215
217
}
216
218
}
217
219
}
0 commit comments