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

Fix panic due to incorrect ANSI escape handling, add fuzzer #137

Merged
merged 4 commits into from Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1061,4 +1061,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());
}
}