@@ -177,17 +177,30 @@ impl HeaderPrefix {
177
177
pub fn decode < R : Buf > ( buf : & mut R ) -> Result < Self , ParseError > {
178
178
let ( _, encoded_insert_count) = prefix_int:: decode ( 8 , buf) ?;
179
179
let ( sign_negative, delta_base) = prefix_int:: decode ( 7 , buf) ?;
180
+
181
+ if encoded_insert_count > ( usize:: MAX as u64 ) {
182
+ return Err ( ParseError :: Integer (
183
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
184
+ ) ) ;
185
+ }
186
+
187
+ if delta_base > ( usize:: MAX as u64 ) {
188
+ return Err ( ParseError :: Integer (
189
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
190
+ ) ) ;
191
+ }
192
+
180
193
Ok ( Self {
181
- encoded_insert_count,
182
- delta_base,
194
+ encoded_insert_count : encoded_insert_count as usize ,
195
+ delta_base : delta_base as usize ,
183
196
sign_negative : sign_negative == 1 ,
184
197
} )
185
198
}
186
199
187
200
pub fn encode < W : BufMut > ( & self , buf : & mut W ) {
188
201
let sign_bit = if self . sign_negative { 1 } else { 0 } ;
189
- prefix_int:: encode ( 8 , 0 , self . encoded_insert_count , buf) ;
190
- prefix_int:: encode ( 7 , sign_bit, self . delta_base , buf) ;
202
+ prefix_int:: encode ( 8 , 0 , self . encoded_insert_count as u64 , buf) ;
203
+ prefix_int:: encode ( 7 , sign_bit, self . delta_base as u64 , buf) ;
191
204
}
192
205
}
193
206
@@ -200,16 +213,32 @@ pub enum Indexed {
200
213
impl Indexed {
201
214
pub fn decode < R : Buf > ( buf : & mut R ) -> Result < Self , ParseError > {
202
215
match prefix_int:: decode ( 6 , buf) ? {
203
- ( 0b11 , i) => Ok ( Indexed :: Static ( i) ) ,
204
- ( 0b10 , i) => Ok ( Indexed :: Dynamic ( i) ) ,
216
+ ( 0b11 , i) => {
217
+ if i > ( usize:: MAX as u64 ) {
218
+ return Err ( ParseError :: Integer (
219
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
220
+ ) ) ;
221
+ }
222
+
223
+ Ok ( Indexed :: Static ( i as usize ) )
224
+ }
225
+ ( 0b10 , i) => {
226
+ if i > ( usize:: MAX as u64 ) {
227
+ return Err ( ParseError :: Integer (
228
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
229
+ ) ) ;
230
+ }
231
+
232
+ Ok ( Indexed :: Dynamic ( i as usize ) )
233
+ }
205
234
( f, _) => Err ( ParseError :: InvalidPrefix ( f) ) ,
206
235
}
207
236
}
208
237
209
238
pub fn encode < W : BufMut > ( & self , buf : & mut W ) {
210
239
match self {
211
- Indexed :: Static ( i) => prefix_int:: encode ( 6 , 0b11 , * i, buf) ,
212
- Indexed :: Dynamic ( i) => prefix_int:: encode ( 6 , 0b10 , * i, buf) ,
240
+ Indexed :: Static ( i) => prefix_int:: encode ( 6 , 0b11 , * i as u64 , buf) ,
241
+ Indexed :: Dynamic ( i) => prefix_int:: encode ( 6 , 0b10 , * i as u64 , buf) ,
213
242
}
214
243
}
215
244
}
@@ -220,13 +249,21 @@ pub struct IndexedWithPostBase(pub usize);
220
249
impl IndexedWithPostBase {
221
250
pub fn decode < R : Buf > ( buf : & mut R ) -> Result < Self , ParseError > {
222
251
match prefix_int:: decode ( 4 , buf) ? {
223
- ( 0b0001 , i) => Ok ( IndexedWithPostBase ( i) ) ,
252
+ ( 0b0001 , i) => {
253
+ if i > ( usize:: MAX as u64 ) {
254
+ return Err ( ParseError :: Integer (
255
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
256
+ ) ) ;
257
+ }
258
+
259
+ Ok ( IndexedWithPostBase ( i as usize ) )
260
+ }
224
261
( f, _) => Err ( ParseError :: InvalidPrefix ( f) ) ,
225
262
}
226
263
}
227
264
228
265
pub fn encode < W : BufMut > ( & self , buf : & mut W ) {
229
- prefix_int:: encode ( 4 , 0b0001 , self . 0 , buf)
266
+ prefix_int:: encode ( 4 , 0b0001 , self . 0 as u64 , buf)
230
267
}
231
268
}
232
269
@@ -253,26 +290,42 @@ impl LiteralWithNameRef {
253
290
254
291
pub fn decode < R : Buf > ( buf : & mut R ) -> Result < Self , ParseError > {
255
292
match prefix_int:: decode ( 4 , buf) ? {
256
- ( f, i) if f & 0b0101 == 0b0101 => Ok ( LiteralWithNameRef :: new_static (
257
- i,
258
- prefix_string:: decode ( 8 , buf) ?,
259
- ) ) ,
260
- ( f, i) if f & 0b0101 == 0b0100 => Ok ( LiteralWithNameRef :: new_dynamic (
261
- i,
262
- prefix_string:: decode ( 8 , buf) ?,
263
- ) ) ,
293
+ ( f, i) if f & 0b0101 == 0b0101 => {
294
+ if i > ( usize:: MAX as u64 ) {
295
+ return Err ( ParseError :: Integer (
296
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
297
+ ) ) ;
298
+ }
299
+
300
+ Ok ( LiteralWithNameRef :: new_static (
301
+ i as usize ,
302
+ prefix_string:: decode ( 8 , buf) ?,
303
+ ) )
304
+ }
305
+ ( f, i) if f & 0b0101 == 0b0100 => {
306
+ if i > ( usize:: MAX as u64 ) {
307
+ return Err ( ParseError :: Integer (
308
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
309
+ ) ) ;
310
+ }
311
+
312
+ Ok ( LiteralWithNameRef :: new_dynamic (
313
+ i as usize ,
314
+ prefix_string:: decode ( 8 , buf) ?,
315
+ ) )
316
+ }
264
317
( f, _) => Err ( ParseError :: InvalidPrefix ( f) ) ,
265
318
}
266
319
}
267
320
268
321
pub fn encode < W : BufMut > ( & self , buf : & mut W ) -> Result < ( ) , prefix_string:: Error > {
269
322
match self {
270
323
LiteralWithNameRef :: Static { index, value } => {
271
- prefix_int:: encode ( 4 , 0b0101 , * index, buf) ;
324
+ prefix_int:: encode ( 4 , 0b0101 , * index as u64 , buf) ;
272
325
prefix_string:: encode ( 8 , 0 , value, buf) ?;
273
326
}
274
327
LiteralWithNameRef :: Dynamic { index, value } => {
275
- prefix_int:: encode ( 4 , 0b0100 , * index, buf) ;
328
+ prefix_int:: encode ( 4 , 0b0100 , * index as u64 , buf) ;
276
329
prefix_string:: encode ( 8 , 0 , value, buf) ?;
277
330
}
278
331
}
@@ -296,16 +349,24 @@ impl LiteralWithPostBaseNameRef {
296
349
297
350
pub fn decode < R : Buf > ( buf : & mut R ) -> Result < Self , ParseError > {
298
351
match prefix_int:: decode ( 3 , buf) ? {
299
- ( f, i) if f & 0b1111_0000 == 0 => Ok ( LiteralWithPostBaseNameRef :: new (
300
- i,
301
- prefix_string:: decode ( 8 , buf) ?,
302
- ) ) ,
352
+ ( f, i) if f & 0b1111_0000 == 0 => {
353
+ if i > ( usize:: MAX as u64 ) {
354
+ return Err ( ParseError :: Integer (
355
+ crate :: qpack:: prefix_int:: Error :: Overflow ,
356
+ ) ) ;
357
+ }
358
+
359
+ Ok ( LiteralWithPostBaseNameRef :: new (
360
+ i as usize ,
361
+ prefix_string:: decode ( 8 , buf) ?,
362
+ ) )
363
+ }
303
364
( f, _) => Err ( ParseError :: InvalidPrefix ( f) ) ,
304
365
}
305
366
}
306
367
307
368
pub fn encode < W : BufMut > ( & self , buf : & mut W ) -> Result < ( ) , prefix_string:: Error > {
308
- prefix_int:: encode ( 3 , 0b0000 , self . index , buf) ;
369
+ prefix_int:: encode ( 3 , 0b0000 , self . index as u64 , buf) ;
309
370
prefix_string:: encode ( 8 , 0 , & self . value , buf) ?;
310
371
Ok ( ( ) )
311
372
}
@@ -347,6 +408,7 @@ impl Literal {
347
408
#[ cfg( test) ]
348
409
mod test {
349
410
use super :: * ;
411
+ use std:: convert:: TryInto ;
350
412
use std:: io:: Cursor ;
351
413
352
414
const TABLE_SIZE : usize = 4096 ;
@@ -424,7 +486,7 @@ mod test {
424
486
#[ test]
425
487
fn base_index_too_small ( ) {
426
488
let mut buf = vec ! [ ] ;
427
- let encoded_largest_ref = ( 2 % ( 2 * TABLE_SIZE / 32 ) ) + 1 ;
489
+ let encoded_largest_ref: u64 = ( ( 2 % ( 2 * TABLE_SIZE / 32 ) ) + 1 ) . try_into ( ) . unwrap ( ) ;
428
490
prefix_int:: encode ( 8 , 0 , encoded_largest_ref, & mut buf) ;
429
491
prefix_int:: encode ( 7 , 1 , 2 , & mut buf) ; // base index negative = 0
430
492
0 commit comments