Skip to content

Commit

Permalink
bump 0.3.15
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Nov 8, 2024
1 parent fe77809 commit 4fbb7b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "Apache-2.0"
name = "sonic-rs"
readme = "README.md"
repository = "https://github.com/cloudwego/sonic-rs"
version = "0.3.14"
version = "0.3.15"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
23 changes: 15 additions & 8 deletions fuzz/fuzz_targets/from_slice.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#![no_main]
#![allow(clippy::mutable_key_type)]
use std::{borrow::Cow, collections::HashMap, hash::Hash, marker::PhantomData};

use faststr::FastStr;
use libfuzzer_sys::fuzz_target;
use serde::{Deserialize, Serialize};
use serde_json::Value as JValue;
use sonic_rs::{
from_slice, from_str, to_array_iter, to_array_iter_unchecked, to_object_iter,
to_object_iter_unchecked, value::JsonContainerTrait, JsonNumberTrait, JsonValueTrait, Value,
to_object_iter_unchecked, value::JsonContainerTrait, Deserializer, JsonNumberTrait,
JsonValueTrait, Value,
};

macro_rules! test_type {
Expand Down Expand Up @@ -54,8 +58,8 @@ fuzz_target!(|data: &[u8]| {
let sv2: Value = from_str(&sout).unwrap();
let eq = compare_value(&jv2, &sv2);

// compare use raw
fuzz_use_raw(data, &sv);
fuzz_utf8_lossy(data, &sv);

if jv.is_object() && eq {
for ret in to_object_iter(data) {
Expand Down Expand Up @@ -119,7 +123,6 @@ fn compare_lazyvalue(jv: &JValue, sv: &sonic_rs::LazyValue) {
}

fn fuzz_use_raw(json: &[u8], sv: &sonic_rs::Value) {
use sonic_rs::{Deserialize, Deserializer, Value};
let json = unsafe { std::str::from_utf8_unchecked(json) };
let mut de = Deserializer::from_str(json).use_raw();
let value: Value = Deserialize::deserialize(&mut de).unwrap();
Expand All @@ -128,6 +131,15 @@ fn fuzz_use_raw(json: &[u8], sv: &sonic_rs::Value) {
assert_eq!(&got, sv);
}

fn fuzz_utf8_lossy(json: &[u8], sv: &sonic_rs::Value) {
let json = unsafe { std::str::from_utf8_unchecked(json) };
let mut de = Deserializer::from_str(json).utf8_lossy();
let value: Value = Deserialize::deserialize(&mut de).unwrap();
let out = sonic_rs::to_string(&value).unwrap();
let got: Value = sonic_rs::from_str(&out).unwrap();
assert_eq!(&got, sv);
}

fn compare_value(jv: &JValue, sv: &sonic_rs::Value) -> bool {
match *jv {
JValue::Object(ref obj) => {
Expand Down Expand Up @@ -175,11 +187,6 @@ fn compare_value(jv: &JValue, sv: &sonic_rs::Value) -> bool {
true
}

use std::{borrow::Cow, collections::HashMap, hash::Hash, marker::PhantomData};

use faststr::FastStr;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Foo {
name: FastStr,
Expand Down
2 changes: 1 addition & 1 deletion scripts/fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -ex

cargo install cargo-fuzz

RUST_BACKTRACE=full cargo +nightly fuzz run fuzz_value
RUST_BACKTRACE=full cargo +nightly fuzz run fuzz_value -- -max_total_time=20m
2 changes: 1 addition & 1 deletion src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ impl<'de, R: Reader<'de>> Deserializer<R> {
// get n to check trailing characters in later
let n = if cfg.utf8_lossy && self.parser.read.next_invalid_utf8() != usize::MAX {
// repr the invalid utf8, not need to care about the invalid UTF8 char in non-string
// parts, it will cause erros when parsing.
// parts, it will cause errors when parsing.
val.parse_with_padding(String::from_utf8_lossy(json).as_bytes(), cfg)?
} else {
val.parse_with_padding(json, cfg)?
Expand Down

0 comments on commit 4fbb7b9

Please sign in to comment.