Skip to content

Commit 1c0f541

Browse files
Merge 0.13.1
1 parent a675443 commit 1c0f541

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

RELEASE-NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
- `DecoderReader` now owns its inner reader, and can expose it via `into_inner()`. For symmetry, `EncoderWriter` can do the same with its writer.
1414
- `encoded_len` is now public so you can size encode buffers precisely.
1515

16+
# 0.13.1
17+
18+
- More precise decode buffer sizing, avoiding unnecessary allocation in `decode_config`.
19+
1620
# 0.13.0
1721

1822
- Config methods are const

src/decode.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ pub fn decode_engine<E: Engine, T: AsRef<[u8]>>(
9595
input: T,
9696
engine: &E,
9797
) -> Result<Vec<u8>, DecodeError> {
98-
let mut buffer = Vec::<u8>::new();
98+
let decoded_length_estimate = (input
99+
.as_ref()
100+
.len()
101+
.checked_add(3)
102+
.expect("decoded length calculation overflow"))
103+
/ 4
104+
* 3;
105+
let mut buffer = Vec::<u8>::with_capacity(decoded_length_estimate);
99106
decode_engine_vec(input, &mut buffer, engine).map(|_| buffer)
100107
}
101108

@@ -340,4 +347,17 @@ mod tests {
340347
assert_eq!(orig_data, decode_buf);
341348
}
342349
}
350+
351+
#[test]
352+
fn decode_engine_estimation_works_for_various_lengths() {
353+
for num_prefix_quads in 0..100 {
354+
for suffix in &["AA", "AAA", "AAAA"] {
355+
let mut prefix = "AAAA".repeat(num_prefix_quads);
356+
prefix.push_str(suffix);
357+
// make sure no overflow (and thus a panic) occurs
358+
let res = decode_engine(prefix, &DEFAULT_ENGINE);
359+
assert!(res.is_ok());
360+
}
361+
}
362+
}
343363
}

0 commit comments

Comments
 (0)