Skip to content

Commit 1669bc0

Browse files
RUST-1992 Make serde an optional feature (#554)
Co-authored-by: Isabel Atkinson <isabelatkinson@gmail.com>
1 parent 5f54ac5 commit 1669bc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1016
-905
lines changed

.evergreen/run-fuzzer.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ run_fuzzer() {
3030
}
3131

3232
# Run existing targets
33-
run_fuzzer "deserialize"
33+
run_fuzzer "decode"
3434
run_fuzzer "raw_deserialize"
35+
run_fuzzer "raw_deserialize_utf8_lossy"
3536
run_fuzzer "iterate"
3637

3738
# Run new security-focused targets
3839
run_fuzzer "type_markers"
3940
run_fuzzer "string_handling"
40-
run_fuzzer "serialization"
41+
run_fuzzer "encoding"

.evergreen/run-tests.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ set -o errexit
44

55
. ~/.cargo/env
66

7-
RUST_BACKTRACE=1 cargo test
7+
# Test with default features and excluding doctests (some of which require the 'serde' feature)
8+
RUST_BACKTRACE=1 cargo test --all-targets
9+
# Test with all features and including doctests
810
RUST_BACKTRACE=1 cargo test --all-features
911

1012
cd serde-tests

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ serde_path_to_error = ["dep:serde_path_to_error"]
4747
# if enabled, include serde_with interop.
4848
# should be used in conjunction with chrono-0_4 or uuid-0_8.
4949
serde_with-3 = ["dep:serde_with"]
50+
serde = ["dep:serde"]
5051

5152
[lib]
5253
name = "bson"
@@ -55,7 +56,7 @@ name = "bson"
5556
ahash = "0.8.0"
5657
chrono = { version = "0.4.15", features = ["std"], default-features = false, optional = true }
5758
rand = "0.9"
58-
serde = { version = "1.0", features = ["derive"] }
59+
serde = { version = "1.0", features = ["derive"], optional = true }
5960
serde_json = { version = "1.0", features = ["preserve_order"] }
6061
indexmap = "2.1.0"
6162
hex = "0.4.2"

examples/deserialize.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/serialize.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/test.bson

-316 Bytes
Binary file not shown.

fuzz/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
target
33
corpus
44
artifacts
5+
Cargo.lock

fuzz/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cargo-fuzz = true
1010

1111
[dependencies.bson]
1212
path = ".."
13+
features = ["serde"]
1314

1415
[dependencies.libfuzzer-sys]
1516
version = "0.4.0"
@@ -24,8 +25,8 @@ version = "1.0"
2425
members = ["."]
2526

2627
[[bin]]
27-
name = "deserialize"
28-
path = "fuzz_targets/deserialize.rs"
28+
name = "decode"
29+
path = "fuzz_targets/decode.rs"
2930

3031
[[bin]]
3132
name = "iterate"
@@ -48,8 +49,8 @@ name = "string_handling"
4849
path = "fuzz_targets/string_handling.rs"
4950

5051
[[bin]]
51-
name = "serialization"
52-
path = "fuzz_targets/serialization.rs"
52+
name = "encoding"
53+
path = "fuzz_targets/encoding.rs"
5354

5455
[[bin]]
5556
name = "generate_corpus"

fuzz/fuzz_targets/deserialize.rs renamed to fuzz/fuzz_targets/decode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use bson::Document;
77
use std::io::Cursor;
88

99
fuzz_target!(|buf: &[u8]| {
10-
if let Ok(doc) = Document::from_reader(&mut Cursor::new(&buf[..])) {
10+
if let Ok(doc) = Document::decode_from_reader(&mut Cursor::new(&buf[..])) {
1111
let mut vec = Vec::with_capacity(buf.len());
12-
let _ = doc.to_writer(&mut vec);
12+
let _ = doc.encode_to_writer(&mut vec);
1313
}
1414
});

fuzz/fuzz_targets/serialization.rs renamed to fuzz/fuzz_targets/encoding.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@ fn compare_values(val1: &Bson, val2: &Bson) -> bool {
4545
}
4646

4747
fuzz_target!(|input: &[u8]| {
48-
if let Ok(rawdoc) = RawDocument::from_bytes(&input) {
48+
if let Ok(rawdoc) = RawDocument::decode_from_bytes(&input) {
4949
if let Ok(doc) = Document::try_from(rawdoc) {
5050
let out = RawDocumentBuf::try_from(&doc).unwrap();
5151
let out_bytes = out.as_bytes();
5252
if input != out_bytes {
53-
let reserialized = RawDocument::from_bytes(&out_bytes).unwrap();
54-
let reserialized_doc = Document::try_from(reserialized).unwrap();
55-
// Ensure that the reserialized document is the same as the original document, the
53+
let reencoded = RawDocument::decode_from_bytes(&out_bytes).unwrap();
54+
let reencoded_doc = Document::try_from(reencoded).unwrap();
55+
// Ensure that the re-encoded document is the same as the original document, the
5656
// bytes can differ while still resulting in the same Document.
57-
if !compare_docs(&doc, &reserialized_doc) {
57+
if !compare_docs(&doc, &reencoded_doc) {
5858
panic!(
59-
"Reserialized document is not the same as the original document: {:?} != \
60-
{:?}",
61-
doc, reserialized_doc
59+
"Reencoded document is not the same as the original document: {:?} != {:?}",
60+
doc, reencoded_doc
6261
);
6362
}
6463
}

0 commit comments

Comments
 (0)