Skip to content

Commit

Permalink
Fix panic due to incorrect ANSI escape handling, add fuzzer (#137)
Browse files Browse the repository at this point in the history
* Fix subtraction with overflow with malformed ANSI escape

* Add fuzzing target

* Add assertion to avoid silent overflow

* Rename fuzzer
  • Loading branch information
5225225 committed Dec 27, 2022
1 parent 25cfe72 commit 3d17077
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 10 deletions.
3 changes: 3 additions & 0 deletions fuzz/.gitignore
@@ -0,0 +1,3 @@
target
corpus
artifacts
267 changes: 267 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions fuzz/Cargo.toml
@@ -0,0 +1,25 @@
[package]
name = "prettytable-rs-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.prettytable-rs]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "render"
path = "fuzz_targets/render.rs"
test = false
doc = false
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/render.rs
@@ -0,0 +1,23 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use prettytable::{Table, Row, Cell};

use prettytable::format::Alignment::{self, *};

fuzz_target!(|data: Vec<Vec<(String, u8)>>| {
fn align_from_u8(x: u8) -> Alignment {
match x {
0 => LEFT,
1 => CENTER,
_ => RIGHT,
}
}
let mut pt = prettytable::Table::new();
for row in data {
let cells = row.into_iter().map(|x| Cell::new_align(&x.0, align_from_u8(x.1))).collect();
pt.add_row(Row::new(cells));
}

let _ = pt.print(&mut std::io::sink());
});
15 changes: 15 additions & 0 deletions src/lib.rs
Expand Up @@ -1072,4 +1072,19 @@ mod tests {
assert!(table.print_html(&mut writer).is_ok());
assert_eq!(writer.as_string().replace("\r\n", "\n"), out);
}

#[test]
fn test_panic() {
let mut table = Table::new();

table.add_row(Row::new(vec![Cell::new("\u{1b}[\u{1b}\u{0}\u{0}")]));

let out = "+--+
| \u{1b}[\u{1b}\u{0}\u{0} |
+--+
";

assert_eq!(table.to_string().replace("\r\n", "\n"), out);
assert_eq!(3, table.print(&mut StringWriter::new()).unwrap());
}
}

0 comments on commit 3d17077

Please sign in to comment.