Skip to content

Commit 1669bc0

Browse files
RUST-1992 Make serde an optional feature (#554)
Co-authored-by: Isabel Atkinson <[email protected]>
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
}

fuzz/fuzz_targets/iterate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate bson;
55
use bson::RawDocument;
66

77
fuzz_target!(|buf: &[u8]| {
8-
if let Ok(doc) = RawDocument::from_bytes(buf) {
8+
if let Ok(doc) = RawDocument::decode_from_bytes(buf) {
99
for _ in doc {}
1010
}
1111
});

fuzz/fuzz_targets/raw_deserialize.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ extern crate bson;
55
use bson::Document;
66

77
fuzz_target!(|buf: &[u8]| {
8-
if let Ok(doc) = bson::from_slice::<Document>(buf) {
9-
let mut vec = Vec::with_capacity(buf.len());
10-
let _ = doc.to_writer(&mut vec);
8+
if let Ok(doc) = bson::deserialize_from_slice::<Document>(buf) {
9+
let _ = bson::serialize_to_vec(&doc);
1110
}
1211
});
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![no_main]
2-
#[macro_use] extern crate libfuzzer_sys;
2+
#[macro_use]
3+
extern crate libfuzzer_sys;
34
extern crate bson;
4-
use bson::Document;
5+
use bson::{serde_helpers::Utf8LossyDeserialization, Document};
56

67
fuzz_target!(|buf: &[u8]| {
7-
if let Ok(doc) = bson::from_slice_utf8_lossy::<Document>(buf) {
8-
let mut vec = Vec::with_capacity(buf.len());
9-
let _ = doc.to_writer(&mut vec);
8+
if let Ok(doc) = bson::deserialize_from_slice::<Utf8LossyDeserialization<Document>>(buf) {
9+
let _ = bson::serialize_to_vec(&doc.0);
1010
}
1111
});

fuzz/fuzz_targets/string_handling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bson::{RawBsonRef, RawDocument};
66
use std::convert::TryInto;
77

88
fuzz_target!(|buf: &[u8]| {
9-
if let Ok(doc) = RawDocument::from_bytes(buf) {
9+
if let Ok(doc) = RawDocument::decode_from_bytes(buf) {
1010
for elem in doc.iter_elements().flatten() {
1111
// Convert to RawBsonRef and check string-related types
1212
if let Ok(bson) = elem.try_into() {

fuzz/fuzz_targets/type_markers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bson::{RawBsonRef, RawDocument};
66
use std::convert::TryInto;
77

88
fuzz_target!(|buf: &[u8]| {
9-
if let Ok(doc) = RawDocument::from_bytes(buf) {
9+
if let Ok(doc) = RawDocument::decode_from_bytes(buf) {
1010
for elem in doc.iter_elements().flatten() {
1111
let _: Result<RawBsonRef, _> = elem.try_into();
1212
}

fuzz/generate_corpus.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ fn generate_length_edge_cases(dir: &Path) -> std::io::Result<()> {
2929
let min_doc = doc! {};
3030
fs::write(
3131
target_dir.join("min_doc"),
32-
bson::to_vec(&min_doc).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
32+
min_doc
33+
.encode_to_vec()
34+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
3335
)?;
3436

3537
// Document with length near i32::MAX
3638
let large_doc = doc! { "a": "b".repeat(i32::MAX as usize / 2) };
3739
fs::write(
3840
target_dir.join("large_doc"),
39-
bson::to_vec(&large_doc).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
41+
large_doc
42+
.encode_to_vec()
43+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
4044
)?;
4145

4246
Ok(())
@@ -73,7 +77,9 @@ fn generate_type_marker_cases(dir: &Path) -> std::io::Result<()> {
7377
};
7478
fs::write(
7579
target_dir.join("all_types"),
76-
bson::to_vec(&all_types).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
80+
all_types
81+
.encode_to_vec()
82+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
7783
)?;
7884

7985
Ok(())
@@ -100,7 +106,9 @@ fn generate_string_edge_cases(dir: &Path) -> std::io::Result<()> {
100106
};
101107
fs::write(
102108
target_dir.join("utf8_cases"),
103-
bson::to_vec(&utf8_cases).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
109+
utf8_cases
110+
.encode_to_vec()
111+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
104112
)?;
105113

106114
Ok(())
@@ -124,7 +132,9 @@ fn generate_serialization_cases(dir: &Path) -> std::io::Result<()> {
124132
}
125133
fs::write(
126134
target_dir.join("nested_doc"),
127-
bson::to_vec(&nested_doc).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
135+
nested_doc
136+
.encode_to_vec()
137+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
128138
)?;
129139

130140
// Document with large binary data
@@ -136,7 +146,9 @@ fn generate_serialization_cases(dir: &Path) -> std::io::Result<()> {
136146
};
137147
fs::write(
138148
target_dir.join("large_binary"),
139-
bson::to_vec(&large_binary).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
149+
large_binary
150+
.encode_to_vec()
151+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
140152
)?;
141153

142154
Ok(())

serde-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2018"
88
default = []
99

1010
[dependencies]
11-
bson = { path = "..", features = ["uuid-1", "chrono-0_4", "serde_with-3"] }
11+
bson = { path = "..", features = ["uuid-1", "chrono-0_4", "serde", "serde_with-3"] }
1212
serde = { version = "1.0", features = ["derive"] }
1313
pretty_assertions = "0.6.1"
1414
hex = "0.4.2"

serde-tests/json.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@ fn owned_raw_bson() {
9999
});
100100

101101
let mut doc_buf = RawDocumentBuf::new();
102-
doc_buf.append("a", "key");
103-
doc_buf.append("number", 12);
104-
doc_buf.append("bool", false);
105-
doc_buf.append("nu", RawBson::Null);
102+
doc_buf.append("a", "key").unwrap();
103+
doc_buf.append("number", 12).unwrap();
104+
doc_buf.append("bool", false).unwrap();
105+
doc_buf.append("nu", RawBson::Null).unwrap();
106106

107107
let mut array_buf = RawArrayBuf::new();
108-
array_buf.push(1);
109-
array_buf.push("string");
108+
array_buf.push(1).unwrap();
109+
array_buf.push("string").unwrap();
110110

111111
let mut bson_doc = RawDocumentBuf::new();
112-
bson_doc.append("first", true);
113-
bson_doc.append("second", "string");
112+
bson_doc.append("first", true).unwrap();
113+
bson_doc.append("second", "string").unwrap();
114114

115115
let expected = Foo {
116116
doc_buf,

0 commit comments

Comments
 (0)