rust-csv icon indicating copy to clipboard operation
rust-csv copied to clipboard

Issue in reader

Open wiluite opened this issue 2 years ago • 3 comments
trafficstars

Possible error in code: file reader.rs

Check code if self.next_class = 255

existing code: if self.next_class > CLASS_SIZE {

probably should be: if self.next_class >= CLASS_SIZE {

const CLASS_SIZE: usize = 256;

    fn new() -> DfaClasses {
        DfaClasses {
            classes: [0; CLASS_SIZE],
            next_class: 1,
        }
    }

    fn add(&mut self, b: u8) {
        if self.next_class > CLASS_SIZE {
            panic!("added too many classes")
        }
        self.classes[b as usize] = self.next_class as u8;
        self.next_class += 1;
    }

May use controlled conversion to u8:

use std::convert::TryInto;
pub fn main() {
    // let mut c = DfaClasses::new();
    let b: u32 = 256;
    //let xxx: u8 = b as u8; // -> ignore overfow
    //let xxx: u8 = b.try_into().unwrap(); // -> runtime error: out of range in conversion
    let xxx: u8 = match b.try_into() {
        Result::Ok(b2) => b2,
        Result::Err(e2) => {
            println!("Error: {}", e2);
            0
        }
    }; // -> Error: out of range integral type conversion attempted
    // c.next_class = 255;
    // println!("{}", c.next_class);
    // c.add(123);
    // println!("{}", c.next_class);
    // c.add(123);
    println!("xxx = {}", xxx);
}

wiluite avatar Dec 18 '22 09:12 wiluite

Good catch. Patches welcome. Note that this error is impossible to trigger though, because the number of classes is always quite small and there is no way to make it bigger.

BurntSushi avatar Dec 18 '22 12:12 BurntSushi

Yes, that's right. But, nobody bothers to do this: const CLASS_SIZE: usize = 3; ;-)

This issue was found by my colleage who just has no github account, so I'd written instead of him, and so I hardly supply a patch (Alas, don't know Rust). But. I have a side question: do you have benchmark statistics of this csv-reader on some of CSV-files (game.csv, for example). Could you give me a link?

wiluite avatar Jan 18 '23 14:01 wiluite

You can run benchmarks with cargo bench.

BurntSushi avatar Jan 18 '23 14:01 BurntSushi