Skip to content

Commit e13ef42

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 cc7774e commit e13ef42

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
@@ -333,6 +333,14 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
333333
return read_uint32(value, 1);
334334
}
335335

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

358363
CborError cbor_parser_init_reader(const struct CborParserOperations *ops, CborParser *parser, CborValue *it, void *token)
359364
{
360-
memset(parser, 0, sizeof(*parser));
365+
cbor_parser_init_common(parser, it);
361366
parser->source.ops = ops;
362367
parser->flags = CborParserFlag_ExternalSource;
363-
it->parser = parser;
364368
it->source.token = token;
365-
it->remaining = 1;
366369
return preparse_value(it);
367370
}
368371

0 commit comments

Comments
 (0)