summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-12-12 12:56:31 +0100
committerTor Andersson <tor.andersson@artifex.com>2016-12-12 13:04:35 +0100
commit88a781b38dc63c02b69c96ef34dc8d6e699de059 (patch)
tree7d2c263af77d05c47f6f6f65e8e281e79e07902b
parentf3820a6d9b9bfd76b4f6e7b8da40d73b2f17ac50 (diff)
downloadmupdf-88a781b38dc63c02b69c96ef34dc8d6e699de059.tar.xz
Ensure we don't clobber files in fz_new_output_with_path.
When saving a PDF to the same file name as the one that is open, we risk clobbering it by truncating the source file. Remove the old file first! Removing an open file will not work on windows, but there we will throw an EACCESS error rather than clobber the file!
-rw-r--r--source/fitz/output.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/source/fitz/output.c b/source/fitz/output.c
index c34b8736..55d2522f 100644
--- a/source/fitz/output.c
+++ b/source/fitz/output.c
@@ -160,6 +160,14 @@ fz_new_output_with_path(fz_context *ctx, const char *filename, int append)
if (!strcmp(filename, "/dev/null") || !fz_strcasecmp(filename, "nul:"))
return NULL;
+ /* Ensure we create a brand new file. We don't want to clobber our old file. */
+ if (!append)
+ {
+ if (remove(filename) < 0)
+ if (errno != ENOENT)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot remove file '%s': %s", filename, strerror(errno));
+ }
+
file = fz_fopen(filename, append ? "ab" : "wb");
if (!file)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));