Skip to content

Commit 94dd5c8

Browse files
committed
FreeImage_Save: do not remove file if copy failed; improved exception safety
1 parent 1ccfb50 commit 94dd5c8

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

Source/FreeImage/Plugin.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -802,22 +802,24 @@ namespace {
802802
}
803803

804804

805-
bool MoveOrCopy(const std::filesystem::path& oldp, const std::filesystem::path& newp)
805+
bool MoveOrCopy(const std::filesystem::path& oldp, const std::filesystem::path& newp) noexcept
806806
{
807807
std::error_code err{};
808808
std::filesystem::rename(oldp, newp, err);
809809
if (!err) {
810810
return true;
811811
}
812-
const auto copied = std::filesystem::copy_file(oldp, newp, std::filesystem::copy_options::overwrite_existing);
812+
const auto copied = std::filesystem::copy_file(oldp, newp, std::filesystem::copy_options::overwrite_existing, err);
813813
if (copied) {
814-
std::filesystem::remove(oldp);
814+
std::filesystem::remove(oldp, err);
815815
}
816816
return copied;
817817
}
818818

819819

820-
FIBOOL SaveImpl(FREE_IMAGE_FORMAT fif, FIBITMAP* dib, const std::filesystem::path& filename, int flags) {
820+
FIBOOL SaveImpl(FREE_IMAGE_FORMAT fif, FIBITMAP* dib, const std::filesystem::path& filename, int flags)
821+
try
822+
{
821823
FreeImageIO io;
822824
SetDefaultIO(&io);
823825

@@ -834,9 +836,9 @@ namespace {
834836
FreeImage_OutputMessageProc((int)fif, "FreeImage_Save: failed to rename output file %s", filename.u8string().c_str());
835837
}
836838
}
837-
838-
if (!success) {
839-
std::filesystem::remove(tmpname);
839+
else {
840+
std::error_code err{};
841+
std::filesystem::remove(tmpname, err);
840842
}
841843
}
842844
else {
@@ -845,6 +847,9 @@ namespace {
845847

846848
return success;
847849
}
850+
catch (...) {
851+
return FALSE;
852+
}
848853

849854
} // namespace
850855

0 commit comments

Comments
 (0)