@@ -21,6 +21,7 @@ pub(crate) struct Blocking<T> {
21
21
pub ( crate ) struct Buf {
22
22
buf : Vec < u8 > ,
23
23
pos : usize ,
24
+ init_len : usize ,
24
25
}
25
26
26
27
pub ( crate ) const DEFAULT_MAX_BUF_SIZE : usize = 2 * 1024 * 1024 ;
@@ -190,6 +191,7 @@ impl Buf {
190
191
Buf {
191
192
buf : Vec :: with_capacity ( n) ,
192
193
pos : 0 ,
194
+ init_len : 0 ,
193
195
}
194
196
}
195
197
@@ -220,6 +222,7 @@ impl Buf {
220
222
let n = cmp:: min ( src. len ( ) , max_buf_size) ;
221
223
222
224
self . buf . extend_from_slice ( & src[ ..n] ) ;
225
+ self . init_len = cmp:: max ( self . init_len , self . buf . len ( ) ) ;
223
226
n
224
227
}
225
228
@@ -236,6 +239,18 @@ impl Buf {
236
239
self . buf . reserve ( len - self . buf . len ( ) ) ;
237
240
}
238
241
242
+ if self . init_len < len {
243
+ debug_assert ! ( self . init_len < self . buf. capacity( ) , "init_len of Vec is bigger than the capacity" ) ;
244
+ debug_assert ! ( len <= self . buf. capacity( ) , "uninit area of Vec is bigger than the capacity" ) ;
245
+
246
+ let uninit_len = len - self . init_len ;
247
+ // SAFETY: the area is within the allocation of the Vec
248
+ unsafe {
249
+ self . buf . as_mut_ptr ( ) . add ( self . init_len ) . write_bytes ( 0 , uninit_len) ;
250
+ }
251
+ }
252
+
253
+ // SAFETY: `len` is within the capacity and is init
239
254
unsafe {
240
255
self . buf . set_len ( len) ;
241
256
}
@@ -287,6 +302,7 @@ cfg_fs! {
287
302
self . buf. extend_from_slice( & buf[ ..len] ) ;
288
303
rem -= len;
289
304
}
305
+ self . init_len = cmp:: max( self . init_len, self . buf. len( ) ) ;
290
306
291
307
max_buf_size - rem
292
308
}
0 commit comments