Error check qoi_read() more strictly

fseek is not guaranteed to succeed and can fail, e.g when trying to
seek a pipe. the first fseek() is not error checked since if it failed,
ftell would return 0.

also check for fread errors too before calling qoi_decode().
This commit is contained in:
NRK 2023-06-15 14:43:24 +06:00
parent 00dfdc8b5c
commit 36190eb07d
1 changed files with 2 additions and 4 deletions

6
qoi.h
View File

@ -627,11 +627,10 @@ void *qoi_read(const char *filename, qoi_desc *desc, int channels) {
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size <= 0) {
if (size <= 0 || fseek(f, 0, SEEK_SET) != 0) {
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_SET);
data = QOI_MALLOC(size);
if (!data) {
@ -641,8 +640,7 @@ void *qoi_read(const char *filename, qoi_desc *desc, int channels) {
bytes_read = fread(data, 1, size, f);
fclose(f);
pixels = qoi_decode(data, bytes_read, desc, channels);
pixels = (bytes_read != size) ? NULL : qoi_decode(data, bytes_read, desc, channels);
QOI_FREE(data);
return pixels;
}