147
147
* \value CborPrettyDefaultFlags Default conversion flags.
148
148
*/
149
149
150
+ static void printRecursionLimit (FILE * out )
151
+ {
152
+ fputs ("<nesting too deep, recursion stopped>" , out );
153
+ }
154
+
150
155
static CborError hexDump (FILE * out , const void * ptr , size_t n )
151
156
{
152
157
const uint8_t * buffer = (const uint8_t * )ptr ;
@@ -273,16 +278,22 @@ static const char *get_indicator(const CborValue *it, int flags)
273
278
return resolve_indicator (it -> ptr , it -> parser -> end , flags );
274
279
}
275
280
276
- static CborError value_to_pretty (FILE * out , CborValue * it , int flags );
277
- static CborError container_to_pretty (FILE * out , CborValue * it , CborType containerType , int flags )
281
+ static CborError value_to_pretty (FILE * out , CborValue * it , int flags , int recursionsLeft );
282
+ static CborError container_to_pretty (FILE * out , CborValue * it , CborType containerType ,
283
+ int flags , int recursionsLeft )
278
284
{
285
+ if (!recursionsLeft ) {
286
+ printRecursionLimit (out );
287
+ return CborNoError ; /* do allow the dumping to continue */
288
+ }
289
+
279
290
const char * comma = "" ;
280
291
while (!cbor_value_at_end (it )) {
281
292
if (fprintf (out , "%s" , comma ) < 0 )
282
293
return CborErrorIO ;
283
294
comma = ", " ;
284
295
285
- CborError err = value_to_pretty (out , it , flags );
296
+ CborError err = value_to_pretty (out , it , flags , recursionsLeft );
286
297
if (err )
287
298
return err ;
288
299
@@ -292,14 +303,14 @@ static CborError container_to_pretty(FILE *out, CborValue *it, CborType containe
292
303
/* map: that was the key, so get the value */
293
304
if (fprintf (out , ": " ) < 0 )
294
305
return CborErrorIO ;
295
- err = value_to_pretty (out , it , flags );
306
+ err = value_to_pretty (out , it , flags , recursionsLeft );
296
307
if (err )
297
308
return err ;
298
309
}
299
310
return CborNoError ;
300
311
}
301
312
302
- static CborError value_to_pretty (FILE * out , CborValue * it , int flags )
313
+ static CborError value_to_pretty (FILE * out , CborValue * it , int flags , int recursionsLeft )
303
314
{
304
315
CborError err ;
305
316
CborType type = cbor_value_get_type (it );
@@ -319,7 +330,7 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
319
330
it -> ptr = recursed .ptr ;
320
331
return err ; /* parse error */
321
332
}
322
- err = container_to_pretty (out , & recursed , type , flags );
333
+ err = container_to_pretty (out , & recursed , type , flags , recursionsLeft - 1 );
323
334
if (err ) {
324
335
it -> ptr = recursed .ptr ;
325
336
return err ; /* parse error */
@@ -424,7 +435,10 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
424
435
err = cbor_value_advance_fixed (it );
425
436
if (err )
426
437
return err ;
427
- err = value_to_pretty (out , it , flags );
438
+ if (recursionsLeft )
439
+ err = value_to_pretty (out , it , flags , recursionsLeft - 1 );
440
+ else
441
+ printRecursionLimit (out );
428
442
if (err )
429
443
return err ;
430
444
if (fprintf (out , ")" ) < 0 )
@@ -533,7 +547,7 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
533
547
*/
534
548
CborError cbor_value_to_pretty_advance (FILE * out , CborValue * value )
535
549
{
536
- return value_to_pretty (out , value , CborPrettyDefaultFlags );
550
+ return value_to_pretty (out , value , CborPrettyDefaultFlags , CBOR_PARSER_MAX_RECURSIONS );
537
551
}
538
552
539
553
/**
@@ -552,7 +566,7 @@ CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
552
566
*/
553
567
CborError cbor_value_to_pretty_advance_flags (FILE * out , CborValue * value , int flags )
554
568
{
555
- return value_to_pretty (out , value , flags );
569
+ return value_to_pretty (out , value , flags , CBOR_PARSER_MAX_RECURSIONS );
556
570
}
557
571
558
572
/** @} */
0 commit comments