From 676e7d7f9b5dc76d5124cc99e3a3fa6c823decc3 Mon Sep 17 00:00:00 2001 From: James Almer Date: Sat, 22 Oct 2022 22:07:34 -0300 Subject: [PATCH] avcodec/adts_parser: allow passing a pre allocated AACADTSHeaderInfo to avpriv_adts_header_parse() Code freeing the struct on failure is kept for backwards compatibility, but should be removed in the next major bump, and the existing lavf user adapted. Signed-off-by: James Almer --- libavcodec/adts_header.c | 2 ++ libavcodec/adts_parser.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libavcodec/adts_header.c b/libavcodec/adts_header.c index ff4efafbf7..00fa0a5a99 100644 --- a/libavcodec/adts_header.c +++ b/libavcodec/adts_header.c @@ -32,6 +32,8 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) int size, rdb, ch, sr; int aot, crc_abs; + memset(hdr, 0, sizeof(*hdr)); + if (get_bits(gbc, 12) != 0xfff) return AAC_AC3_PARSE_ERROR_SYNC; diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c index 4a1a8fd5f4..f2e155fc99 100644 --- a/libavcodec/adts_parser.c +++ b/libavcodec/adts_parser.c @@ -47,24 +47,30 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_ { #if CONFIG_ADTS_HEADER int ret = 0; + int allocated = 0; GetBitContext gb; if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE) return AVERROR_INVALIDDATA; - *phdr = av_mallocz(sizeof(AACADTSHeaderInfo)); + if (!*phdr) { + allocated = 1; + *phdr = av_mallocz(sizeof(AACADTSHeaderInfo)); + } if (!*phdr) return AVERROR(ENOMEM); ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE); if (ret < 0) { - av_freep(phdr); + if (allocated) + av_freep(phdr); return ret; } ret = ff_adts_header_parse(&gb, *phdr); if (ret < 0) { - av_freep(phdr); + if (allocated) + av_freep(phdr); return ret; }