From 597b88b1181534f8c7516eb1d4d3efb19c4bcc1e Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Thu, 11 May 2023 23:44:46 +0900 Subject: [PATCH] jpg_loader: Add file existence check Prevent memory leaks. ==2277==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f71c4ff2587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104 #1 0x7f71c4a8d56a in jpgdHeader(char const*, int*, int*) ../src/loaders/jpg/tvgJpgd.cpp:2930 #2 0x7f71c4ad4f85 in JpgLoader::open(std::__cxx11::basic_string, std::allocator > const&) ../src/loaders/jpg/tvgJpgLoader.cpp:59 #3 0x7f71c497d663 in LoaderMgr::loader(std::__cxx11::basic_string, std::allocator > const&, bool*) ../src/lib/tvgLoader.cpp:174 #4 0x7f71c4992d81 in tvg::Picture::Impl::load(std::__cxx11::basic_string, std::allocator > const&) ../src/lib/tvgPictureImpl.h:220 #5 0x7f71c498aa86 in tvg::Picture::load(std::__cxx11::basic_string, std::allocator > const&) ../src/lib/tvgPicture.cpp:58 #6 0x55df238cb293 in C_A_T_C_H_T_E_S_T_24 ../test/testPicture.cpp:371 #7 0x55df2366d499 in Catch::RunContext::invokeActiveTestCase() ../test/catch.hpp:13025 #8 0x55df236dee1b in Catch::RunContext::runCurrentTest(std::__cxx11::basic_string, std::allocator >&, std::__cxx11::basic_string, std::allocator >&) ../test/catch.hpp:12998 #9 0x55df23710586 in Catch::RunContext::runTest(Catch::TestCase const&) ../test/catch.hpp:12759 #10 0x55df237252d4 in execute ../test/catch.hpp:13352 #11 0x55df237252d4 in Catch::Session::runInternal() ../test/catch.hpp:13562 #12 0x55df23726b86 in Catch::Session::run() ../test/catch.hpp:13518 #13 0x55df235f7179 in int Catch::Session::run(int, char const* const*) ../test/catch.hpp:13236 #14 0x55df235f7179 in main ../test/catch.hpp:17543 #15 0x7f71c3791082 in __libc_start_main ../csu/libc-start.c:308 SUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s). --- src/loaders/jpg/tvgJpgd.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/loaders/jpg/tvgJpgd.cpp b/src/loaders/jpg/tvgJpgd.cpp index 6ea2efb0..76210fda 100644 --- a/src/loaders/jpg/tvgJpgd.cpp +++ b/src/loaders/jpg/tvgJpgd.cpp @@ -2928,10 +2928,14 @@ jpeg_decoder* jpgdHeader(const char* data, int size, int* width, int* height) jpeg_decoder* jpgdHeader(const char* filename, int* width, int* height) { auto fileStream = new jpeg_decoder_file_stream(); - if (!fileStream->open(filename)) return nullptr; + if (!fileStream->open(filename)) { + delete(fileStream); + return nullptr; + } auto decoder = new jpeg_decoder(fileStream); if (decoder->get_error_code() != JPGD_SUCCESS) { + delete(fileStream); delete(decoder); return nullptr; }