File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 13
13
- ` DecoderReader ` now owns its inner reader, and can expose it via ` into_inner() ` . For symmetry, ` EncoderWriter ` can do the same with its writer.
14
14
- ` encoded_len ` is now public so you can size encode buffers precisely.
15
15
16
+ # 0.13.1
17
+
18
+ - More precise decode buffer sizing, avoiding unnecessary allocation in ` decode_config ` .
19
+
16
20
# 0.13.0
17
21
18
22
- Config methods are const
Original file line number Diff line number Diff line change @@ -95,7 +95,14 @@ pub fn decode_engine<E: Engine, T: AsRef<[u8]>>(
95
95
input : T ,
96
96
engine : & E ,
97
97
) -> 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) ;
99
106
decode_engine_vec ( input, & mut buffer, engine) . map ( |_| buffer)
100
107
}
101
108
@@ -340,4 +347,17 @@ mod tests {
340
347
assert_eq ! ( orig_data, decode_buf) ;
341
348
}
342
349
}
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
+ }
343
363
}
You can’t perform that action at this time.
0 commit comments