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().
This commit is contained in:
NRK 2023-06-15 14:32:33 +06:00
parent dfc056e813
commit 00dfdc8b5c
1 changed files with 4 additions and 2 deletions

6
qoi.h
View File

@ -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) {