@@ -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,27 @@ impl Buf {
236
239
self . buf . reserve ( len - self . buf . len ( ) ) ;
237
240
}
238
241
242
+ if self . init_len < len {
243
+ debug_assert ! (
244
+ self . init_len < self . buf. capacity( ) ,
245
+ "init_len of Vec is bigger than the capacity"
246
+ ) ;
247
+ debug_assert ! (
248
+ len <= self . buf. capacity( ) ,
249
+ "uninit area of Vec is bigger than the capacity"
250
+ ) ;
251
+
252
+ let uninit_len = len - self . init_len ;
253
+ // SAFETY: the area is within the allocation of the Vec
254
+ unsafe {
255
+ self . buf
256
+ . as_mut_ptr ( )
257
+ . add ( self . init_len )
258
+ . write_bytes ( 0 , uninit_len) ;
259
+ }
260
+ }
261
+
262
+ // SAFETY: `len` is within the capacity and is init
239
263
unsafe {
240
264
self . buf . set_len ( len) ;
241
265
}
@@ -287,6 +311,7 @@ cfg_fs! {
287
311
self . buf. extend_from_slice( & buf[ ..len] ) ;
288
312
rem -= len;
289
313
}
314
+ self . init_len = cmp:: max( self . init_len, self . buf. len( ) ) ;
290
315
291
316
max_buf_size - rem
292
317
}
0 commit comments