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

Bug in Row implementation with regex matching #111

Open
antmelon opened this issue Oct 22, 2024 · 2 comments
Open

Bug in Row implementation with regex matching #111

antmelon opened this issue Oct 22, 2024 · 2 comments

Comments

@antmelon
Copy link

Performing a row filter with a regex that does not exist in CSV file will panic and crash

impl Row {
    pub fn subset(&self, indices: &[usize]) -> Row {
        let mut subfields = vec![];
        for i in indices {
            subfields.push(self.fields.get(*i).unwrap().clone());
        }
        Row {
            record_num: self.record_num,
            fields: subfields,
        }
    }

    fn empty() -> Row {
        Row {
            record_num: 0,
            fields: vec![],
        }
    }
}

the for loop in subset will try to unwrap a None value and crash

@YS-L
Copy link
Owner

YS-L commented Oct 26, 2024

The subset method is intended for filtering columns rather than rows. The indices used are generated here:

csvlens/src/view.rs

Lines 41 to 51 in 29e46af

let mut indices = vec![];
let mut filtered_headers: Vec<String> = vec![];
for (i, header) in headers.iter().enumerate() {
if pattern.is_match(header) {
indices.push(i);
filtered_headers.push(header.clone());
}
}
let disabled_because_no_match;
if indices.is_empty() {
indices = (0..headers.len()).collect();

This logic ensures the indices are always valid.

Applying a row filter doesn't trigger these code paths. Have you been able to reproduce the panic? Sharing a traceback could help identify any underlying cause.

@antmelon
Copy link
Author

antmelon commented Oct 29, 2024

The issue happens when there are rows with different numbers of columns.
foo.csv

When filtering on a column that is present for all rows, there are no issues. When filtering on a column that is either out of bounds of some rows, or not present in any, the following error is received:

thread 'main' panicked at /home/alongo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/csvlens-0.10.1/src/csv.rs:96:48:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: csvlens::csv::Row::subset
   5: csvlens::view::RowsView::do_get_rows
   6: csvlens::view::RowsView::set_columns_filter
   7: csvlens::app::App::set_columns_filter
   8: csvlens::app::App::main_loop
   9: csvlens::runner::AppRunner::run
  10: csvlens::runner::run_csvlens
  11: csvlens::main

The change made in #112 resolves this issue and allows filtering on all columns. If the column is not present in any row, it functions as normal and continues to show all columns

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

2 participants