@@ -251,6 +251,36 @@ static inline CborError validate_simple_type(uint8_t simple_type, int flags)
251
251
CborErrorUnknownSimpleType : CborNoError ;
252
252
}
253
253
254
+ static inline CborError validate_number (const CborValue * it , CborType type , int flags )
255
+ {
256
+ CborError err = CborNoError ;
257
+ const uint8_t * ptr = it -> ptr ;
258
+ uint64_t value ;
259
+
260
+ if ((flags & CborValidateShortestIntegrals ) == 0 )
261
+ return err ;
262
+ if (type >= CborHalfFloatType && type <= CborDoubleType )
263
+ return err ; /* checked elsewhere */
264
+
265
+ err = _cbor_value_extract_number (& ptr , it -> parser -> end , & value );
266
+ if (err )
267
+ return err ;
268
+
269
+ size_t bytesUsed = (size_t )(ptr - it -> ptr - 1 );
270
+ size_t bytesNeeded = 0 ;
271
+ if (value >= Value8Bit )
272
+ ++ bytesNeeded ;
273
+ if (value > 0xffU )
274
+ ++ bytesNeeded ;
275
+ if (value > 0xffffU )
276
+ bytesNeeded += 2 ;
277
+ if (value > 0xffffffffU )
278
+ bytesNeeded += 4 ;
279
+ if (bytesNeeded < bytesUsed )
280
+ return CborErrorOverlongEncoding ;
281
+ return CborNoError ;
282
+ }
283
+
254
284
static inline CborError validate_tag (CborValue * it , CborTag tag , int flags , int recursionLeft )
255
285
{
256
286
CborType type = cbor_value_get_type (it );
@@ -391,12 +421,17 @@ static CborError validate_container(CborValue *it, int containerType, int flags,
391
421
static CborError validate_value (CborValue * it , int flags , int recursionLeft )
392
422
{
393
423
CborError err ;
394
- if (flags & CborValidateNoIndeterminateLength ) {
395
- if (!cbor_value_is_length_known (it ))
424
+ CborType type = cbor_value_get_type (it );
425
+
426
+ if (cbor_value_is_length_known (it )) {
427
+ err = validate_number (it , type , flags );
428
+ if (err )
429
+ return err ;
430
+ } else {
431
+ if (flags & CborValidateNoIndeterminateLength )
396
432
return CborErrorUnknownLength ;
397
433
}
398
434
399
- CborType type = cbor_value_get_type (it );
400
435
switch (type ) {
401
436
case CborArrayType :
402
437
case CborMapType : {
@@ -428,7 +463,15 @@ static CborError validate_value(CborValue *it, int flags, int recursionLeft)
428
463
size_t n = 0 ;
429
464
const void * ptr ;
430
465
466
+ err = _cbor_value_prepare_string_iteration (it );
467
+ if (err )
468
+ return err ;
469
+
431
470
while (1 ) {
471
+ err = validate_number (it , type , flags );
472
+ if (err )
473
+ return err ;
474
+
432
475
err = _cbor_value_get_string_chunk (it , & ptr , & n , it );
433
476
if (err )
434
477
return err ;
0 commit comments