Skip to content

Crash due to subtract with overflow #130

Closed
@alexanderkjall

Description

@alexanderkjall

Hi, I did some fuzzing of a crates that uses this library, and found the following crash:

thread 'main' panicked at 'attempt to subtract with overflow', src/utils.rs:106:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/397b390cc76ba1d98f80b2a24a371f708dcc9169/library/std/src/panicking.rs:475
   1: core::panicking::panic_fmt
             at /rustc/397b390cc76ba1d98f80b2a24a371f708dcc9169/library/core/src/panicking.rs:85
   2: core::panicking::panic
             at /rustc/397b390cc76ba1d98f80b2a24a371f708dcc9169/library/core/src/panicking.rs:50
   3: prettytable::utils::display_width
             at ./src/utils.rs:106
   4: prettytable::cell::Cell::new_align
             at ./src/cell.rs:30
   5: prettytable::cell::Cell::new
             at ./src/cell.rs:47
   6: crash::main::{{closure}}::{{closure}}
             at ./examples/crash.rs:17
   7: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:280
   8: core::option::Option<T>::map
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:437
   9: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::next
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:844
  10: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec.rs:2116
  11: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec.rs:2018
  12: core::iter::traits::iterator::Iterator::collect
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1653
  13: crash::main::{{closure}}
             at ./examples/crash.rs:17
  14: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:280
  15: core::option::Option<T>::map
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:437
  16: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::next
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:844
  17: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec.rs:2116
  18: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec.rs:2018
  19: core::iter::traits::iterator::Iterator::collect
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1653
  20: crash::main
             at ./examples/crash.rs:15
  21: core::ops::function::FnOnce::call_once
             at /home/capitol/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227

It can be reproduced with the following program:

use prettytable::{csv::ReaderBuilder, Table, Row, Cell, format};

use std::io::Cursor;

fn main() {
    let data: Vec<u8> = vec![125, 27, 27, 27, 91, 27, 91, 125, 91, 27, 10, 9];
    let reader = Box::new(Cursor::new(data));

    let csv_reader = &mut ReaderBuilder::new()
        .delimiter('\t' as u8)
        .has_headers(false)
        .from_reader(reader);

    let mut table = Table::init(
        csv_reader
            .records()
            .map(|row| Row::new(row.unwrap().into_iter().map(|cell| Cell::new(&cell)).collect()))
            .collect(),
    );
    table.set_format(format::TableFormat::new());
    table.printstd();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions