Skip to content

Commit e2c5480

Browse files
committed
cborparser: Move parser initialisation to common routine.
`cbor_parser_init` and `cbor_parser_init_reader` are substantially similar, however the latter misses clearing out `it->flags`, leaving it uninitialised so possibly unsafe. Rather than copying & pasting that from `cbor_parser_init`, lets just use one routine that does the "common" part, then each routine can focus on the specifics needed.
1 parent bad81be commit e2c5480

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/cborparser.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
332332
return read_uint32(value, 1);
333333
}
334334

335+
static void cbor_parser_init_common(CborParser *parser, CborValue *it)
336+
{
337+
memset(parser, 0, sizeof(*parser));
338+
it->parser = parser;
339+
it->remaining = 1; /* there's one type altogether, usually an array or map */
340+
it->flags = 0;
341+
}
342+
335343
/**
336344
* Initializes the CBOR parser for parsing \a size bytes beginning at \a
337345
* buffer. Parsing will use flags set in \a flags. The iterator to the first
@@ -344,24 +352,19 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
344352
*/
345353
CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
346354
{
347-
memset(parser, 0, sizeof(*parser));
355+
cbor_parser_init_common(parser, it);
348356
parser->source.end = buffer + size;
349357
parser->flags = (enum CborParserGlobalFlags)flags;
350-
it->parser = parser;
351358
it->source.ptr = buffer;
352-
it->remaining = 1; /* there's one type altogether, usually an array or map */
353-
it->flags = 0;
354359
return preparse_value(it);
355360
}
356361

357362
CborError cbor_parser_init_reader(const struct CborParserOperations *ops, CborParser *parser, CborValue *it, void *token)
358363
{
359-
memset(parser, 0, sizeof(*parser));
364+
cbor_parser_init_common(parser, it);
360365
parser->source.ops = ops;
361366
parser->flags = CborParserFlag_ExternalSource;
362-
it->parser = parser;
363367
it->source.token = token;
364-
it->remaining = 1;
365368
return preparse_value(it);
366369
}
367370

0 commit comments

Comments
 (0)