From 00dfdc8b5c941287341b9dbc9e65a0d3c4fd4aa4 Mon Sep 17 00:00:00 2001 From: NRK Date: Thu, 15 Jun 2023 14:32:33 +0600 Subject: [PATCH] Error check qoi_write() more strictly simply checking the return value of fwrite() wouldn't be enough since stdio is typically buffered. and so force a flush and check for errors via ferror(). --- qoi.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qoi.h b/qoi.h index 6734ac46..eb01232a 100644 --- a/qoi.h +++ b/qoi.h @@ -594,7 +594,7 @@ void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { int qoi_write(const char *filename, const void *data, const qoi_desc *desc) { FILE *f = fopen(filename, "wb"); - int size; + int size, err; void *encoded; if (!f) { @@ -608,10 +608,12 @@ int qoi_write(const char *filename, const void *data, const qoi_desc *desc) { } fwrite(encoded, 1, size, f); + fflush(f); + err = ferror(f); fclose(f); QOI_FREE(encoded); - return size; + return err ? 0 : size; } void *qoi_read(const char *filename, qoi_desc *desc, int channels) {