Skip to content

Rust 0.9 Segmentation fault in BufferedReader #11997

Closed
@hdima

Description

@hdima

I use Rust 0.9 on Ubuntu 12.04.4 LTS x86_64:

$ rustc -v
rustc 0.9 (7613b15 2014-01-08 18:04:43 -0800)
host: x86_64-unknown-linux-gnu

I'm not sure if the following code is a correct way to do it but it's surely shouldn't crash with segmentation fault:

use std::os;
use std::iter::Iterator;
use std::io::Buffer;
use std::io::fs::File;
use std::io::buffered::BufferedReader;

struct Parser<'r> {
    buffer: &'r mut Buffer
}

impl<'r> Parser<'r> {
    fn new(filename: &str) -> Parser<'r> {
        let path = Path::new(filename);
        let file = File::open(&path).unwrap();
        println!("HERE");
        let mut reader = BufferedReader::new(file);
        Parser{buffer: &mut reader}
    }
}

impl<'r> Iterator<~str> for Parser<'r> {
    fn next(&mut self) -> Option<~str> {
        self.buffer.read_line()
    }
}

fn main() {
    let args = os::args();
    let mut parser = Parser::new(args[1]);
    for line in parser {
        print!("Line: {}", line);
    }
}

The code compiles without any errors with the following command:

$ rustc -Z debug-info parser.rs

If println! on line 15 is commented out the program will print the following error:

$ ./parser parser.rs 
task '<main>' failed at 'assertion failed: end <= self.len()', .../rust/src/libstd/vec.rs:1027

Otherwise it will crash with segmentation fault:

$ ./parser parser.rs 
HERE
Segmentation fault (core dumped)

And gdb returns the following stack trace:

(gdb) bt
#0  0x000000000041f9f3 in io::buffered::Buffer$BufferedReader::fill::hf78f7f1290b9d770gQaB::v0.0 ()
#1  0x00000000004252e8 in io::Buffer::anon::expr_fn::at ()
#2  0x00000000004200a1 in condition::Trap::inside::h05e3bde831ab23e82NaU::v0.0 ()
#3  0x000000000041fe2b in io::Buffer::read_until::h7807419d82baeae9aQ::v0.0 ()
#4  0x000000000041fbcb in io::Buffer::read_line::hd50542f84924c5a2aH::v0.0 () at parser.rs:17
#5  0x00000000004291f5 in parser::Iterator$Parser::next () at parser.rs:23
#6  0x0000000000429335 in parser::main () at parser.rs:30
#7  0x00000000004f8c98 in task::__extensions__::build_start_wrapper::anon::anon::expr_fn::a4 ()
#8  0x00000000004ccaa8 in rt::task::__extensions__::run::anon::expr_fn::an ()
#9  0x00000000004d402c in rust_try ()
#10 0x00000000004cc89d in rt::task::Task::run::hf32c3cb15c069866Qpak::v0.9 ()
#11 0x00000000004f86bc in task::__extensions__::build_start_wrapper::anon::expr_fn::aN ()
#12 0x0000000000000000 in ?? ()
Warning: the current language does not match this frame.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions