avformat/tests/fifo_muxer: Fix leak of AVPacket on error

Also factor allocating and freeing the packet out.
Fixes Coverity issues #1473722 and #1473723; it is a regression
since 4b386b2059.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2021-03-19 01:24:28 +01:00
parent ea1e15fdc4
commit aff1d373b1
1 changed files with 15 additions and 22 deletions

View File

@ -55,7 +55,7 @@ static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data,
return ret;
}
static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc, AVPacket **pkt)
{
int ret = 0;
AVStream *s;
@ -71,22 +71,20 @@ static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
if (!s) {
fprintf(stderr, "Failed to create stream: %s\n",
av_err2str(ret));
ret = AVERROR(ENOMEM);
return AVERROR(ENOMEM);
}
return ret;
*pkt = av_packet_alloc();
if (!*pkt)
return AVERROR(ENOMEM);
return 0;
}
static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
const FailingMuxerPacketData *pkt_data)
AVPacket *pkt, const FailingMuxerPacketData *pkt_data)
{
int ret = 0, i;
AVPacket *pkt;
pkt = av_packet_alloc();
if (!pkt)
return AVERROR(ENOMEM);
ret = avformat_write_header(oc, opts);
if (ret) {
@ -107,11 +105,9 @@ static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
if (ret < 0) {
fprintf(stderr, "Unexpected write_frame error: %s\n",
av_err2str(ret));
av_packet_free(&pkt);
goto write_trailer_and_fail;
}
}
av_packet_free(&pkt);
ret = av_write_frame(oc, NULL);
if (ret < 0) {
@ -135,15 +131,10 @@ fail:
}
static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
const FailingMuxerPacketData *data)
AVPacket *pkt, const FailingMuxerPacketData *data)
{
int ret = 0, i;
int64_t write_pkt_start, write_pkt_end, duration;
AVPacket *pkt;
pkt = av_packet_alloc();
if (!pkt)
return AVERROR(ENOMEM);
ret = avformat_write_header(oc, opts);
if (ret) {
@ -166,7 +157,6 @@ static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
break;
}
}
av_packet_free(&pkt);
write_pkt_end = av_gettime_relative();
duration = write_pkt_end - write_pkt_start;
@ -193,7 +183,8 @@ fail:
}
typedef struct TestCase {
int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
int (*test_func)(AVFormatContext *, AVDictionary **,
AVPacket *, const FailingMuxerPacketData *pkt_data);
const char *test_name;
const char *options;
@ -211,10 +202,11 @@ static int run_test(const TestCase *test)
{
AVDictionary *opts = NULL;
AVFormatContext *oc = NULL;
AVPacket *pkt = NULL;
char buffer[BUFFER_SIZE];
int ret, ret1;
ret = initialize_fifo_tst_muxer_chain(&oc);
ret = initialize_fifo_tst_muxer_chain(&oc, &pkt);
if (ret < 0) {
fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
goto end;
@ -240,11 +232,12 @@ static int run_test(const TestCase *test)
goto end;
}
ret = test->test_func(oc, &opts, &test->pkt_data);
ret = test->test_func(oc, &opts, pkt, &test->pkt_data);
end:
printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
avformat_free_context(oc);
av_packet_free(&pkt);
av_dict_free(&opts);
return ret;
}