From 5e067e6f3db1a6f42dfb3812ea8b6663c9369727 Mon Sep 17 00:00:00 2001 From: wantehchang Date: Mon, 23 Jul 2018 17:53:34 -0700 Subject: [PATCH] Miscellaneous cleanups. (#1648) - Close 'file' before returning on errors. - Don't call aom_codec_destroy() after an aom_codec_dec_init() failure. - Remove a cast to unsigned int* that's no longer necessary. - Change NULL to nullptr. --- projects/libaom/av1_dec_fuzzer.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/projects/libaom/av1_dec_fuzzer.cc b/projects/libaom/av1_dec_fuzzer.cc index b9d68f615..c8f72e5f8 100644 --- a/projects/libaom/av1_dec_fuzzer.cc +++ b/projects/libaom/av1_dec_fuzzer.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include "aom/aom_decoder.h" #include "aom/aomdx.h" @@ -11,16 +12,19 @@ static const char *const kIVFSignature = "DKIF"; +static void close_file(FILE *file) { fclose(file); } + extern "C" void usage_exit(void) { exit(EXIT_FAILURE); } extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - FILE *const file = fmemopen((void *)data, size, "rb"); + std::unique_ptr file( + fmemopen((void *)data, size, "rb"), &close_file); if (file == nullptr) { return 0; } char header[32]; - if (fread(header, 1, 32, file) != 32) { + if (fread(header, 1, 32, file.get()) != 32) { return 0; } const AvxInterface *decoder = get_aom_decoder_by_name("av1"); @@ -29,7 +33,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { } aom_codec_ctx_t codec; - #if defined(DECODE_MODE) const int threads = 1; #elif defined(DECODE_MODE_threaded) @@ -39,7 +42,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { #endif aom_codec_dec_cfg_t cfg = {threads, 0, 0}; if (aom_codec_dec_init(&codec, decoder->codec_interface(), &cfg, 0)) { - aom_codec_destroy(&codec); return 0; } @@ -48,9 +50,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { uint8_t *buffer = nullptr; size_t buffer_size = 0; size_t frame_size = 0; - while (!ivf_read_frame(file, &buffer, &frame_size, &buffer_size, NULL)) { - const aom_codec_err_t err = aom_codec_decode( - &codec, buffer, static_cast(frame_size), NULL); + while (!ivf_read_frame(file.get(), &buffer, &frame_size, &buffer_size, + nullptr)) { + const aom_codec_err_t err = + aom_codec_decode(&codec, buffer, frame_size, nullptr); ++frame_in_cnt; aom_codec_iter_t iter = nullptr; aom_image_t *img = nullptr; @@ -59,7 +62,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { } } aom_codec_destroy(&codec); - fclose(file); free(buffer); return 0; }