From 88a781b38dc63c02b69c96ef34dc8d6e699de059 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 12 Dec 2016 12:56:31 +0100 Subject: 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! --- source/fitz/output.c | 8 ++++++++ 1 file changed, 8 insertions(+) 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)); -- cgit v1.2.3