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:
parent
00dfdc8b5c
commit
36190eb07d
6
qoi.h
6
qoi.h
@ -627,11 +627,10 @@ void *qoi_read(const char *filename, qoi_desc *desc, int channels) {
|
|||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
size = ftell(f);
|
size = ftell(f);
|
||||||
if (size <= 0) {
|
if (size <= 0 || fseek(f, 0, SEEK_SET) != 0) {
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fseek(f, 0, SEEK_SET);
|
|
||||||
|
|
||||||
data = QOI_MALLOC(size);
|
data = QOI_MALLOC(size);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@ -641,8 +640,7 @@ void *qoi_read(const char *filename, qoi_desc *desc, int channels) {
|
|||||||
|
|
||||||
bytes_read = fread(data, 1, size, f);
|
bytes_read = fread(data, 1, size, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
pixels = (bytes_read != size) ? NULL : qoi_decode(data, bytes_read, desc, channels);
|
||||||
pixels = qoi_decode(data, bytes_read, desc, channels);
|
|
||||||
QOI_FREE(data);
|
QOI_FREE(data);
|
||||||
return pixels;
|
return pixels;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user