Skip to content

Unsafe behaviour without unsafe code, bug in lifetime checker / shared memory restrictions? #19552

Closed
@ztdwu

Description

@ztdwu
#![feature( if_let )]
use std::io::BufReader;

fn main() {
    let text = b"the
                 quick
                 brown
                 fox
                 jumps
                 over";
    let mut reader = BufReader::new(text);

    let (tx, rx) = channel();

    spawn(proc() {
        for line in reader.lines() {
            let line = line.unwrap();
            let line = line.trim_chars(|c: char| c.is_whitespace());
            let list = vec![ line ];
            if let [ word ] = &*list {
                tx.send(word);
            }
        }
    });

    for word in rx.iter() {
        println!("{}", word);
    }
}

One would expect the output to be

the
quick
brown
fox
jumps
over

But the actual output is:

the
overs
overs
ove
overs
over

It looks like the memory location at word is overwritten.
The program will behave correctly if word is deep copied before being sent, i.e. changing tx.send(word) to tx.send(word.to_string())

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions