Fix potential handle leak

Fix potential handle leak

Issue: #I6AUHK
Signed-off-by: qiuyu <qiuyu22@huawei.com>
Change-Id: Ic2d17aa1414b1d2307f97d704a74d3fcf05b95c7
This commit is contained in:
qiuyu 2023-01-17 11:17:16 +08:00
parent b30ad02ceb
commit ed203b793f
4 changed files with 13 additions and 0 deletions

View File

@ -219,6 +219,7 @@ std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location,
auto open_error = OpenArchiveFile(zipfile, fp); auto open_error = OpenArchiveFile(zipfile, fp);
if (open_error != ZIPARCHIVE_OK) { if (open_error != ZIPARCHIVE_OK) {
LOG(ERROR, PANDAFILE) << "Can't open archive " << location; LOG(ERROR, PANDAFILE) << "Can't open archive " << location;
fclose(fp);
return nullptr; return nullptr;
} }
bool try_default = archive_filename.empty(); bool try_default = archive_filename.empty();
@ -242,6 +243,7 @@ std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location,
if (GetCurrentFileInfo(zipfile, &entry) != ZIPARCHIVE_OK) { if (GetCurrentFileInfo(zipfile, &entry) != ZIPARCHIVE_OK) {
OpenPandaFileFromZipErrorHandler(zipfile); OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "GetCurrentFileInfo error"; LOG(ERROR, PANDAFILE) << "GetCurrentFileInfo error";
fclose(fp);
return nullptr; return nullptr;
} }
// check that file is not empty, otherwise crash at CloseArchiveFile // check that file is not empty, otherwise crash at CloseArchiveFile
@ -249,12 +251,14 @@ std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location,
OpenPandaFileFromZipErrorHandler(zipfile); OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Invalid panda file '" << (try_default ? ARCHIVE_FILENAME : archive_filename) LOG(ERROR, PANDAFILE) << "Invalid panda file '" << (try_default ? ARCHIVE_FILENAME : archive_filename)
<< "'"; << "'";
fclose(fp);
return nullptr; return nullptr;
} }
if (OpenCurrentFile(zipfile) != ZIPARCHIVE_OK) { if (OpenCurrentFile(zipfile) != ZIPARCHIVE_OK) {
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
OpenPandaFileFromZipErrorHandler(zipfile); OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Can't OpenCurrentFile!"; LOG(ERROR, PANDAFILE) << "Can't OpenCurrentFile!";
fclose(fp);
return nullptr; return nullptr;
} }
GetCurrentFileOffset(zipfile, &entry); GetCurrentFileOffset(zipfile, &entry);
@ -262,6 +266,7 @@ std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location,
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
if (panda::CloseArchiveFile(zipfile) != 0) { if (panda::CloseArchiveFile(zipfile) != 0) {
LOG(ERROR, PANDAFILE) << "CloseArchive failed!"; LOG(ERROR, PANDAFILE) << "CloseArchive failed!";
fclose(fp);
return nullptr; return nullptr;
} }
} else { } else {

View File

@ -295,6 +295,7 @@ HWTEST(File, CheckHeader, testing::ext::TestSize.Level0)
os::mem::MMAP_FLAG_PRIVATE, writer.GetOffset()).ToConst(); os::mem::MMAP_FLAG_PRIVATE, writer.GetOffset()).ToConst();
EXPECT_NE(ptr.Get(), nullptr); EXPECT_NE(ptr.Get(), nullptr);
EXPECT_TRUE(CheckHeader(ptr, ABC_FILE)); EXPECT_TRUE(CheckHeader(ptr, ABC_FILE));
fclose(fp);
remove(ABC_FILE); remove(ABC_FILE);
} }

View File

@ -230,6 +230,7 @@ static void UnzipFileCheckTxt(const char *archivename, char *filename, const cha
os::mem::UnmapRaw(mem, size_to_mmap); os::mem::UnmapRaw(mem, size_to_mmap);
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
CloseArchive(zipfile); CloseArchive(zipfile);
fclose(myfile);
ASSERT_EQ(1, 0) << "ExtractToMemory() failed!, uncompressed_length is " << uncompressed_length - 1 ASSERT_EQ(1, 0) << "ExtractToMemory() failed!, uncompressed_length is " << uncompressed_length - 1
<< ", original strlen is " << dlen; << ", original strlen is " << dlen;
return; return;
@ -239,6 +240,7 @@ static void UnzipFileCheckTxt(const char *archivename, char *filename, const cha
os::mem::UnmapRaw(mem, size_to_mmap); os::mem::UnmapRaw(mem, size_to_mmap);
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
CloseArchive(zipfile); CloseArchive(zipfile);
fclose(myfile);
ASSERT_EQ(1, 0) << "ExtractToMemory() memcmp failed!"; ASSERT_EQ(1, 0) << "ExtractToMemory() memcmp failed!";
return; return;
} }
@ -460,6 +462,7 @@ static void UnzipFileCheckInDirectory(const char *archivename, char *filename, c
os::mem::UnmapRaw(mem, size_to_mmap); os::mem::UnmapRaw(mem, size_to_mmap);
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
CloseArchive(zipfile); CloseArchive(zipfile);
fclose(myfile);
ASSERT_EQ(1, 0) << "ExtractToMemory() failed!, uncompressed_length is " << uncompressed_length - 1 ASSERT_EQ(1, 0) << "ExtractToMemory() failed!, uncompressed_length is " << uncompressed_length - 1
<< ", original strlen is " << dlen; << ", original strlen is " << dlen;
return; return;
@ -469,6 +472,7 @@ static void UnzipFileCheckInDirectory(const char *archivename, char *filename, c
os::mem::UnmapRaw(mem, size_to_mmap); os::mem::UnmapRaw(mem, size_to_mmap);
CloseCurrentFile(zipfile); CloseCurrentFile(zipfile);
CloseArchive(zipfile); CloseArchive(zipfile);
fclose(myfile);
ASSERT_EQ(1, 0) << "ExtractToMemory() memcmp failed!"; ASSERT_EQ(1, 0) << "ExtractToMemory() memcmp failed!";
return; return;
} }

View File

@ -55,16 +55,19 @@ void OpenUncompressedArchiveFuzzTest(const uint8_t *data, size_t size)
} }
if (panda::LocateFile(zipfile, filename) != panda::ZIPARCHIVE_OK) { if (panda::LocateFile(zipfile, filename) != panda::ZIPARCHIVE_OK) {
CloseAndRemoveZipFile(zipfile, fp, zip_filename); CloseAndRemoveZipFile(zipfile, fp, zip_filename);
(void)fclose(fp);
return; return;
} }
panda::EntryFileStat entry; panda::EntryFileStat entry;
if (panda::GetCurrentFileInfo(zipfile, &entry) != panda::ZIPARCHIVE_OK) { if (panda::GetCurrentFileInfo(zipfile, &entry) != panda::ZIPARCHIVE_OK) {
CloseAndRemoveZipFile(zipfile, fp, zip_filename); CloseAndRemoveZipFile(zipfile, fp, zip_filename);
(void)fclose(fp);
return; return;
} }
if (panda::OpenCurrentFile(zipfile) != panda::ZIPARCHIVE_OK) { if (panda::OpenCurrentFile(zipfile) != panda::ZIPARCHIVE_OK) {
panda::CloseCurrentFile(zipfile); panda::CloseCurrentFile(zipfile);
CloseAndRemoveZipFile(zipfile, fp, zip_filename); CloseAndRemoveZipFile(zipfile, fp, zip_filename);
(void)fclose(fp);
return; return;
} }
panda::GetCurrentFileOffset(zipfile, &entry); panda::GetCurrentFileOffset(zipfile, &entry);