Skip to content

Fix multibyte char after value #15

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

Merged
merged 2 commits into from
May 23, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
177 changes: 177 additions & 0 deletions fuzz/Cargo.lock

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

21 changes: 21 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "json-five-fuzz"
version = "0.0.0"
publish = false
edition = "2024"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.json-five]
path = ".."

[[bin]]
name = "fuzz_target_1"
path = "fuzz_targets/fuzz_target_1.rs"
test = false
doc = false
bench = false
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use json_five::tokenize::tokenize_bytes;
use json_five::parser::from_tokens;
fuzz_target!(|data: &[u8]| {
let result = tokenize_bytes(data);
if let Ok(tokens) = result {
let _ = from_tokens(&tokens);
}
});
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl<'toks, 'input> JSON5Parser<'toks, 'input> {
None => {}
Some(span) => {
if span.1 != TokType::EOF {
return Err(self.make_error(format!("Unexpected {:?} token after value", span.1), span.0 - 1))
return Err(self.make_error(format!("Unexpected {:?} token after value", span.1), span.0))
}
}
}
Expand Down Expand Up @@ -600,6 +600,12 @@ mod tests {
use crate::parser::JSONValue::*;
use super::*;

#[test]
fn test_fuzz_1() {
let res = from_str("0xA18 {9");
assert!(res.is_err());
}

#[test]
fn test_from_bytes() {
let res = from_bytes(b"{}").unwrap();
Expand Down
10 changes: 9 additions & 1 deletion src/rt/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ impl<'toks, 'input> JSON5Parser<'toks, 'input> {
None => {}
Some(span) => {
if span.1 != TokType::EOF {
return Err(self.make_error(format!("Unexpected {:?} token after value", span.1), span.0 - 1))
return Err(self.make_error(format!("Unexpected {:?} token after value", span.1), span.0))
}
}
}
Expand Down Expand Up @@ -704,7 +704,15 @@ pub fn from_str(source: &str) -> Result<JSONText, ParsingError> {
#[cfg(test)]
mod tests {
use crate::tokenize::Tokenizer;

use super::*;

#[test]
fn test_fuzz_1() {
let res = from_str("0xA18 {9");
assert!(res.is_err());
}

#[test]
fn test_foo() {
let res = from_str("{}").unwrap();
Expand Down