Skip to content

Commit 930a44c

Browse files
authored
Fix CBOR fuzzing timeout (#384)
* early return for map and array comparison * leaner ordering assignment
1 parent 1adde22 commit 930a44c

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

libraries/cbor/src/values.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
//! Types for expressing CBOR values.
1616
17-
use super::writer::write;
1817
use alloc::boxed::Box;
1918
use alloc::string::{String, ToString};
2019
use alloc::vec::Vec;
@@ -124,16 +123,34 @@ impl Ord for Value {
124123
(ByteString(b1), ByteString(b2)) => b1.len().cmp(&b2.len()).then(b1.cmp(b2)),
125124
(TextString(t1), TextString(t2)) => t1.len().cmp(&t2.len()).then(t1.cmp(t2)),
126125
(Array(a1), Array(a2)) if a1.len() != a2.len() => a1.len().cmp(&a2.len()),
126+
(Array(a1), Array(a2)) => {
127+
// Arrays of same length.
128+
let mut ordering = Ordering::Equal;
129+
for (e1, e2) in a1.iter().zip(a2.iter()) {
130+
ordering = e1.cmp(e2);
131+
if !matches!(ordering, Ordering::Equal) {
132+
break;
133+
}
134+
}
135+
ordering
136+
}
127137
(Map(m1), Map(m2)) if m1.len() != m2.len() => m1.len().cmp(&m2.len()),
138+
(Map(m1), Map(m2)) => {
139+
// Maps of same length.
140+
let mut ordering = Ordering::Equal;
141+
for ((k1, v1), (k2, v2)) in m1.iter().zip(m2.iter()) {
142+
ordering = k1.cmp(k2).then_with(|| v1.cmp(v2));
143+
if !matches!(ordering, Ordering::Equal) {
144+
break;
145+
}
146+
}
147+
ordering
148+
}
128149
(Tag(t1, v1), Tag(t2, v2)) => t1.cmp(t2).then(v1.cmp(v2)),
129150
(Simple(s1), Simple(s2)) => s1.cmp(s2),
130-
(v1, v2) => {
131-
// This case could handle all of the above as well. Checking individually is faster.
132-
let mut encoding1 = Vec::new();
133-
let _ = write(v1.clone(), &mut encoding1);
134-
let mut encoding2 = Vec::new();
135-
let _ = write(v2.clone(), &mut encoding2);
136-
encoding1.cmp(&encoding2)
151+
(_, _) => {
152+
// The case of different major types is caught above.
153+
unreachable!();
137154
}
138155
}
139156
}

0 commit comments

Comments
 (0)