From 1e65984613179f739ada239368b63fb93957fa03 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Thu, 3 Jun 2021 14:00:16 +0800 Subject: [PATCH 1/3] test: add fuzzer Signed-off-by: Chojan Shang --- fuzz/.gitignore | 3 +++ fuzz/Cargo.toml | 17 ++++++++++++++ fuzz/README.md | 35 +++++++++++++++++++++++++++++ fuzz/fuzz_targets/fuzz_parse_sql.rs | 12 ++++++++++ 4 files changed, 67 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/README.md create mode 100644 fuzz/fuzz_targets/fuzz_parse_sql.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 000000000..9c64849b8 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,3 @@ +corpus +hfuzz_target +hfuzz_workspace diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 000000000..72ab86ef6 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "fuzz" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +honggfuzz = "0.5.54" +sqlparser = { path = ".." } + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "fuzz_parse_sql" +path = "fuzz_targets/fuzz_parse_sql.rs" diff --git a/fuzz/README.md b/fuzz/README.md new file mode 100644 index 000000000..d63f06b0e --- /dev/null +++ b/fuzz/README.md @@ -0,0 +1,35 @@ +# fuzz + +## Installing `honggfuzz` + +``` +cargo install honggfuzz +``` + +Install [dependencies](https://github.com/rust-fuzz/honggfuzz-rs#dependencies) for your system. + +## Fuzzing + +Choose a target. +These are `[[bin]]` entries in `Cargo.toml`. +List them with `cargo read-manifest | jq '.targets[].name'` from the `fuzz` directory. + +Run the fuzzer: + +```shell +cd fuzz +cargo hfuzz run +``` + +After a panic is found, get a stack trace with: + +```shell +cargo hfuzz run-debug hfuzz_workspace//*.fuzz +``` + +For example, with the `fuzz_parse_sql` target: + +```shell +cargo hfuzz run fuzz_parse_sql +cargo hfuzz run-debug fuzz_parse_sql hfuzz_workspace/fuzz_parse_sql/*.fuzz +``` diff --git a/fuzz/fuzz_targets/fuzz_parse_sql.rs b/fuzz/fuzz_targets/fuzz_parse_sql.rs new file mode 100644 index 000000000..629fa360b --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_parse_sql.rs @@ -0,0 +1,12 @@ +use honggfuzz::fuzz; +use sqlparser::dialect::GenericDialect; +use sqlparser::parser::Parser; + +fn main() { + loop { + fuzz!(|data: String| { + let dialect = GenericDialect {}; + let _ = Parser::parse_sql(&dialect, &data); + }); + } +} From 0c52491191f1ea58e698d620bc53920acee97b2f Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Thu, 3 Jun 2021 14:10:15 +0800 Subject: [PATCH 2/3] docs: fuzzing Signed-off-by: Chojan Shang --- fuzz/README.md => docs/fuzzing.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) rename fuzz/README.md => docs/fuzzing.md (84%) diff --git a/fuzz/README.md b/docs/fuzzing.md similarity index 84% rename from fuzz/README.md rename to docs/fuzzing.md index d63f06b0e..72d3d96eb 100644 --- a/fuzz/README.md +++ b/docs/fuzzing.md @@ -1,4 +1,4 @@ -# fuzz +# Fuzzing ## Installing `honggfuzz` @@ -8,9 +8,12 @@ cargo install honggfuzz Install [dependencies](https://github.com/rust-fuzz/honggfuzz-rs#dependencies) for your system. -## Fuzzing +## Running the fuzzer + +Running the fuzzer is as easy as running in the `fuzz` directory. + +Choose a target: -Choose a target. These are `[[bin]]` entries in `Cargo.toml`. List them with `cargo read-manifest | jq '.targets[].name'` from the `fuzz` directory. From a12dd0e83a06ef6bf707199df84003f9412b504a Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Fri, 25 Jun 2021 03:43:11 +0800 Subject: [PATCH 3/3] *: make clippy happy Signed-off-by: Chojan Shang --- src/parser.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 863fc66d0..266bc0c62 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2202,8 +2202,8 @@ impl<'a> Parser<'a> { Ok(Query { with, body, - limit, order_by, + limit, offset, fetch, }) @@ -2358,8 +2358,7 @@ impl<'a> Parser<'a> { ]) // This couldn't possibly be a bad idea })? .into_iter() - .filter(|i| i.is_some()) - .map(|i| i.unwrap()) + .flatten() .collect(); lateral_views.push(LateralView { @@ -2414,8 +2413,8 @@ impl<'a> Parser<'a> { top, projection, from, - selection, lateral_views, + selection, group_by, cluster_by, distribute_by,