Skip to content

Commit baff7fa

Browse files
committed
Merge #239: fix(fuzz): avoid keywords in identifiers
6384adc fix(fuzz): avoid keywords in identifiers (Volodymyr Herashchenko) Pull request description: Fixes #236. Sometimes the fuzzer would generate keywords in identifiers and parser would reject this. This PR simply pads any keyword with `_`, like it was done with other built-ins. ACKs for top commit: apoelstra: ACK 6384adc; successfully ran local tests Tree-SHA512: 09a1b0c5ba75541f54eacb6f6f9fb5cf4b3a971586d2646a360c9c8819d653a2841d8f54fa69babb748c679807959d5504c8ef8554f983e24642e5a3b7f1e18e
2 parents fdcb8c0 + 6384adc commit baff7fa

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/lexer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ pub fn lex<'src>(input: &'src str) -> (Option<Tokens<'src>>, Vec<crate::error::R
237237
)
238238
}
239239

240+
/// Checks whether a given string is a keyword.
241+
#[cfg(feature = "arbitrary")]
242+
pub fn is_keyword(s: &str) -> bool {
243+
matches!(
244+
s,
245+
"fn" | "let" | "type" | "mod" | "const" | "match" | "true" | "false"
246+
)
247+
}
248+
240249
#[cfg(test)]
241250
mod tests {
242251
use chumsky::error::Rich;

src/str.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ macro_rules! wrapped_string {
4747
/// Implementation of [`arbitrary::Arbitrary`] for wrapped string types,
4848
/// such that strings of 1 to 10 letters `a` to `z` are generated.
4949
///
50-
/// The space of lowercase letter strings includes values that are invalid
51-
/// according to the grammar of the particular string type. For instance,
52-
/// keywords are reserved. However, this should not affect fuzzing.
50+
/// The space of lowercase letter strings includes reserved keywords,
51+
/// which cannot be used as identifiers. To ensure valid grammar
52+
/// for fuzzing, any generated keywords are padded with the `_`.
5353
macro_rules! impl_arbitrary_lowercase_alpha {
5454
($wrapper:ident) => {
5555
#[cfg(feature = "arbitrary")]
@@ -61,6 +61,9 @@ macro_rules! impl_arbitrary_lowercase_alpha {
6161
let offset = u.int_in_range(0..=25)?;
6262
string.push((b'a' + offset) as char)
6363
}
64+
if crate::lexer::is_keyword(string.as_str()) {
65+
string.push('_');
66+
}
6467
Ok(Self::from_str_unchecked(string.as_str()))
6568
}
6669
}
@@ -103,7 +106,7 @@ impl<'a> arbitrary::Arbitrary<'a> for FunctionName {
103106
let offset = u.int_in_range(0..=25)?;
104107
string.push((b'a' + offset) as char)
105108
}
106-
if RESERVED_NAMES.contains(&string.as_str()) {
109+
if RESERVED_NAMES.contains(&string.as_str()) || crate::lexer::is_keyword(string.as_str()) {
107110
string.push('_');
108111
}
109112

@@ -202,7 +205,7 @@ impl<'a> arbitrary::Arbitrary<'a> for AliasName {
202205
let offset = u.int_in_range(0..=25)?;
203206
string.push((b'a' + offset) as char)
204207
}
205-
if RESERVED_NAMES.contains(&string.as_str()) {
208+
if RESERVED_NAMES.contains(&string.as_str()) || crate::lexer::is_keyword(string.as_str()) {
206209
string.push('_');
207210
}
208211

0 commit comments

Comments
 (0)