Fix memory leak when realloc() fails

This commit is contained in:
Luflosi 2020-02-20 10:54:10 +01:00
parent eb65aca1f4
commit 5c83f45bc6
No known key found for this signature in database
GPG key ID: 4E41E29EDCC345D0

View file

@ -260,10 +260,12 @@ png_path_to_bitmap(const char* path, uint8_t** data, unsigned int* width, unsign
while (!feof(fp)) {
if (pos - capacity < 1024) {
capacity *= 2;
buf = realloc(buf, capacity);
if (!buf) {
unsigned char *new_buf = realloc(buf, capacity);
if (!new_buf) {
free(buf);
log_error("Out of memory reading PNG file at: %s", path); fclose(fp); return false;
}
buf = new_buf;
}
pos += fread(buf + pos, sizeof(char), capacity - pos, fp);
int saved_errno = errno;