speedy icon indicating copy to clipboard operation
speedy copied to clipboard

Segfault on &'a [&'a str]

Open alfa07 opened this issue 1 year ago • 0 comments

Speedy segfaults on this one:

use speedy::{Readable, Writable};

#[derive(Readable, Writable, Debug)]
struct Item<'a> {
    tags: &'a [&'a str],
    time: u32,
    value: f32,
}

// #[derive(Readable, Writable, Debug)]
// struct Item<'a> {
//     tags: Vec<&'a str>,
//     time: u32,
//     value: f32,
// }

fn main() {
    let mut buf = Vec::new();
    gen(&mut buf);
    println!("buf.len: {}", buf.len());
    let mut rbuf = buf.as_slice();
    while !rbuf.is_empty() {
        let (ritem, n) = Item::read_with_length_from_buffer(rbuf);
        println!("n: {}", n);
        println!("{:?}", ritem);
        let item = ritem.unwrap();
        println!("{:?}", item);
        rbuf = &rbuf[n..];
        println!("rbuf.len: {}", rbuf.len());
    }
}

fn gen<W: std::io::Write>(mut w: W) {
    for i in 0..10 {
        let item = Item {
            tags: &["tag1", "tag2"],
            time: i,
            value: 3.14,
        };
        item.write_to_stream(&mut w).unwrap();
    }
}

If I change tags to Vec<&'a str> it works.

❯ uname -a
Darwin MacBook-Pro.local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:10 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6031 arm64

❯ rustc --version
rustc 1.75.0 (82e1608df 2023-12-21)

alfa07 avatar Mar 03 '24 20:03 alfa07