From 94974653c1929b0e12f388a4255c9dd2563eaa08 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Sun, 28 Nov 2021 17:59:51 +0100 Subject: [PATCH] Lock output file before writing; close #18 --- qoi.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/qoi.h b/qoi.h index d169d68..0aec728 100644 --- a/qoi.h +++ b/qoi.h @@ -584,20 +584,21 @@ void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { #include int qoi_write(const char *filename, const void *data, const qoi_desc *desc) { - int size; - void *encoded = qoi_encode(data, desc, &size); - if (!encoded) { + FILE *f = fopen(filename, "wb"); + if (!f) { return 0; } - FILE *f = fopen(filename, "wb"); - if (!f) { - QOI_FREE(encoded); + int size; + void *encoded = qoi_encode(data, desc, &size); + if (!encoded) { + fclose(f); return 0; - } + } fwrite(encoded, 1, size, f); fclose(f); + QOI_FREE(encoded); return size; }