Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: added `crates.io` badge + rudimentary fuzzing for emails and URLS

Elijah Potterc7f0581a99ac
diff --git a/Cargo.lock b/Cargo.lock
index 0fc62907f..5c19d5b84 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -535,6 +535,7 @@ dependencies = [
"once_cell",
"paste",
"pulldown-cmark",
+ "rand",
"serde",
"smallvec",
"thiserror",
@@ -970,6 +971,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@@ -1024,6 +1031,36 @@ dependencies = [
"proc-macro2",
]
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
[[package]]
name = "redox_syscall"
version = "0.4.1"
diff --git a/README.md b/README.md
index 48bfddc0d..faedc4239 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,7 @@
[![Harper LS](https://github.com/elijah-potter/harper/actions/workflows/build_harper_ls.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/build_harper_ls.yml)
[![Web](https://github.com/elijah-potter/harper/actions/workflows/build_web.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/build_web.yml)
[![Precommit](https://github.com/elijah-potter/harper/actions/workflows/precommit.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/precommit.yml)
+[![Crates.io](https://img.shields.io/crates/v/harper-ls)](https://crates.io/crates/harper-ls)
Harper is an English grammar checker designed to be _just right._
I created it after years of dealing with the shortcomings of the competition.
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index 1c32b1080..f82af5ab9 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -21,6 +21,7 @@ thiserror = "1.0.56"
[dev-dependencies]
divan = "0.1.14"
+rand = "0.8.5"
[[bench]]
name = "parse_demo"
diff --git a/harper-core/src/lexing/email_address.rs b/harper-core/src/lexing/email_address.rs
index bd859d450..6244d7b22 100644
--- a/harper-core/src/lexing/email_address.rs
+++ b/harper-core/src/lexing/email_address.rs
@@ -18,7 +18,7 @@ pub fn lex_email_address(source: &[char]) -> Option<FoundToken> {
Some(FoundToken {
next_index: at_loc + 1 + domain_part_len,
- token: TokenKind::EmailAddress
+ token: TokenKind::EmailAddress,
})
}
@@ -90,7 +90,7 @@ fn valid_unquoted_character(c: char) -> bool {
let others = [
'!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}',
- '~', '.'
+ '~', '.',
];
if others.contains(&c) {
@@ -102,6 +102,8 @@ fn valid_unquoted_character(c: char) -> bool {
#[cfg(test)]
mod tests {
+ use rand::Rng;
+
use super::super::hostname::tests::example_domain_parts;
use super::{lex_email_address, validate_local_part};
@@ -123,7 +125,7 @@ mod tests {
r#"user-"#,
r#"postmaster"#,
r#"postmaster"#,
- r#"_test"#
+ r#"_test"#,
]
.into_iter()
.map(|s| s.chars().collect())
@@ -152,4 +154,18 @@ mod tests {
}
}
}
+
+ /// Tests that the email parser will not throw a panic under some random situations.
+ #[test]
+ fn survives_random_chars() {
+ let mut rng = rand::thread_rng();
+
+ let mut buf = [' '; 128];
+
+ for _ in 0..1 << 16 {
+ rng.try_fill(&mut buf).unwrap();
+
+ lex_email_address(&buf);
+ }
+ }
}
diff --git a/harper-core/src/lexing/url.rs b/harper-core/src/lexing/url.rs
index 007df7e53..4b14fb6b9 100644
--- a/harper-core/src/lexing/url.rs
+++ b/harper-core/src/lexing/url.rs
@@ -14,7 +14,7 @@ pub fn lex_url(source: &[char]) -> Option<FoundToken> {
Some(FoundToken {
next_index: url_end + sep + 1,
- token: TokenKind::Url
+ token: TokenKind::Url,
})
}
@@ -96,7 +96,7 @@ fn lex_hostport(source: &[char]) -> Option<usize> {
c.is_ascii_digit()
})
.map(|(i, _)| i)
- .unwrap_or(source.len())
+ .unwrap_or(source.len()),
)
} else {
Some(hostname_end)
@@ -196,6 +196,8 @@ fn lex_uchar(source: &[char]) -> Option<usize> {
#[cfg(test)]
mod tests {
+ use rand::Rng;
+
use super::lex_url;
fn assert_consumes_full(url: &str) {
@@ -232,4 +234,18 @@ mod tests {
fn consumes_with_path() {
assert_consumes_full("https://elijahpotter.dev/articles/quantifying_hope_on_a_global_scale")
}
+
+ /// Tests that the URL parser will not throw a panic under some random situations.
+ #[test]
+ fn survives_random_chars() {
+ let mut rng = rand::thread_rng();
+
+ let mut buf = [' '; 128];
+
+ for _ in 0..1 << 16 {
+ rng.try_fill(&mut buf).unwrap();
+
+ lex_url(&buf);
+ }
+ }
}
diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rs
index afb9ccf87..1e96c531f 100644
--- a/harper-core/src/linting/an_a.rs
+++ b/harper-core/src/linting/an_a.rs
@@ -16,7 +16,7 @@ impl Linter for AnA {
let is_a_an = match chars_first {
['a'] => Some(true),
['a', 'n'] => Some(false),
- _ => None
+ _ => None,
};
let Some(a_an) = is_a_an else {
@@ -28,7 +28,7 @@ impl Linter for AnA {
if a_an != should_be_a_an {
let replacement = match a_an {
true => vec!['a', 'n'],
- false => vec!['a']
+ false => vec!['a'],
};
lints.push(Lint {
@@ -36,7 +36,7 @@ impl Linter for AnA {
lint_kind: LintKind::Formatting,
suggestions: vec![Suggestion::ReplaceWith(replacement)],
message: "This is not vocally correct.".to_string(),
- priority: 31
+ priority: 31,
})
}
}