Skip to content

Overflow in std/io/mem.rs "combine" returns incorrect error with large positions #20678

Closed
@MichaelGG

Description

@MichaelGG

The combine function at https://github.com/rust-lang/rust/blob/7d8d06f86b48520814596bd5363d2b82bc619774/src/libstd/io/mem.rs , line 25, overflows by casting the parameters "cur" and "end" from uint to i64. Apart from using very large arrays on true 64-bit architectures, this bug can be triggered by MemReader's consume function, which simply increments the position.

use std::io::{MemReader,SeekStyle,Seek,Buffer};
fn main() {
    let mut mr = MemReader::new(vec![]);
    mr.consume((1u64 << 63) as uint);
    println!("{}", mr.seek(0, SeekStyle::SeekCur));
}

When run this produces: Err(invalid seek to a negative offset). This is an incorrect error as the seek does not lead to a negative offset.

I would expect that all uses of combine should check their inputs before to make sure they're within 63-bit addressing range, or combine should correctly handle seeking at large positions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions