Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust 0.9 Segmentation fault in BufferedReader #11997

Closed
hdima opened this issue Feb 2, 2014 · 2 comments
Closed

Rust 0.9 Segmentation fault in BufferedReader #11997

hdima opened this issue Feb 2, 2014 · 2 comments

Comments

@hdima
Copy link
Contributor

hdima commented Feb 2, 2014

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.
@zkamsler
Copy link
Contributor

zkamsler commented Feb 2, 2014

Parser::new should not compile, since it is returning a struct containing a borrowed pointer to a BufferedReader that will not last beyond the end of the function.

This looks like a dupe of #11374

@alexcrichton
Copy link
Member

Agreed dupe of #11374, closing.

flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 28, 2023
…-pedantic, r=xFrednet

Move `uninhabited_references` to `nursery`

I think this lint has too many false positives and should be put in pedantic. See rust-lang#11984 and rust-lang#11985 for context.

The lint is already in beta and is causing trouble for us, so I would also like this PR to be backported to beta as well.

changelog: Moved [`uninhabited_references`] to `nursery` (Now allow-by-default)
[rust-lang#11997](rust-lang/rust-clippy#11997)
(Check if this has been backported)

Fixes rust-lang#11984.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants