Skip to content

Preserve destination files when saving fails - #165

Merged
danoli3 merged 2 commits into
masterfrom
fix/save-error-handling
Oct 15, 2026
Merged

danoli3 merged 2 commits into
masterfrom
fix/save-error-handling

Conversation

@danoli3

@danoli3 danoli3 commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

FreeImage_Save previously opened the destination with w+b before encoding, so an encoder failure destroyed an existing file. Stage output in an exclusively created sibling temporary file and replace the destination only after encoding and fclose succeed. Failed encodes, stream errors, close failures, and rename failures clean up the temporary file.

Adapt the staged-save approach described in FreeImageRe 4.2.0 without importing its C++20 filesystem implementation. Keep narrow and Windows Unicode entry points; SaveToHandle remains caller-owned. POSIX special files and symlink destinations are rejected. New POSIX files are private (0600); existing regular-file permissions are preserved. Replacement changes hard-link semantics and is not a power-loss durability guarantee.

Tests: a custom encoder writes partial output then fails; the pre-fix library replaces the original with incomplete, while the fixed build preserves it. Test missing destinations, invalid formats, header-only saves, successful PNG replacement, and Windows Unicode paths. Update the old header-only test to check Save's return value rather than reading a stale destination. macOS C++11 AddressSanitizer build and all three CTest suites pass. Windows and Linux coverage runs in existing CI.

PR stack

Root PR, based on master. Next: #166.

#165 → #166 → #167 → #168 → #169 → #170 → #171

Merge bottom-up. Each PR diff is relative to its predecessor. Combined stack validation passed locally: 10 CTest suites with ASan, 9 in a shared build, 6 with both optional codecs disabled, plus all four Python/NumPy test groups. GitHub CI reruns after the branch rewrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant