diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2020-12-22 13:43:33 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2020-12-22 13:43:33 +0300 |
commit | 512a23c3d61d3ac454e9c098ddbf1d0d3beb19a0 (patch) | |
tree | 9789970ec75cb3c4941c70d2e510f8479b0084d2 /source/blender/sequencer/intern/image_cache.c | |
parent | 0e85d701c6546cc80b7c1f8e089ab34f14095d3a (diff) |
VSE: handle IO errors when reading disk cache header and version
Respond to return values of `fscanf()` and `fread()` to detect and
handle I/O errors. Not only is error handling a good idea, this also
prevents warnings from GCC that `fread()` and `fscanf()` return values
are ignored.
Reviewed By: ISS
Differential Revision: https://developer.blender.org/D9915
Diffstat (limited to 'source/blender/sequencer/intern/image_cache.c')
-rw-r--r-- | source/blender/sequencer/intern/image_cache.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/source/blender/sequencer/intern/image_cache.c b/source/blender/sequencer/intern/image_cache.c index 40ad70cf9a0..a24fa4fe298 100644 --- a/source/blender/sequencer/intern/image_cache.c +++ b/source/blender/sequencer/intern/image_cache.c @@ -425,7 +425,10 @@ static void seq_disk_cache_handle_versioning(SeqDiskCache *disk_cache) FILE *file = BLI_fopen(path_version_file, "r"); if (file) { - fscanf(file, "%d", &version); + const int num_items_read = fscanf(file, "%d", &version); + if (num_items_read == 0) { + version = -1; + } fclose(file); } @@ -510,10 +513,14 @@ static size_t inflate_file_to_imbuf(ImBuf *ibuf, FILE *file, DiskCacheHeaderEntr ibuf->rect_float, header_entry->size_raw, file, header_entry->offset); } -static void seq_disk_cache_read_header(FILE *file, DiskCacheHeader *header) +static bool seq_disk_cache_read_header(FILE *file, DiskCacheHeader *header) { fseek(file, 0, 0); - fread(header, sizeof(*header), 1, file); + const size_t num_items_read = fread(header, sizeof(*header), 1, file); + if (num_items_read < 1) { + perror("unable to read disk cache header"); + return false; + } for (int i = 0; i < DCACHE_IMAGES_PER_FILE; i++) { if ((ENDIAN_ORDER == B_ENDIAN) && header->entry[i].encoding == 0) { @@ -523,6 +530,8 @@ static void seq_disk_cache_read_header(FILE *file, DiskCacheHeader *header) BLI_endian_switch_uint64(&header->entry[i].size_raw); } } + + return true; } static size_t seq_disk_cache_write_header(FILE *file, DiskCacheHeader *header) @@ -611,8 +620,12 @@ static bool seq_disk_cache_write_file(SeqDiskCache *disk_cache, SeqCacheKey *key DiskCacheHeader header; memset(&header, 0, sizeof(header)); - seq_disk_cache_read_header(file, &header); + if (!seq_disk_cache_read_header(file, &header)) { + fclose(file); + return false; + } int entry_index = seq_disk_cache_add_header_entry(key, ibuf, &header); + size_t bytes_written = deflate_imbuf_to_file( ibuf, file, seq_disk_cache_compression_level(), &header.entry[entry_index]); @@ -644,7 +657,10 @@ static ImBuf *seq_disk_cache_read_file(SeqDiskCache *disk_cache, SeqCacheKey *ke return NULL; } - seq_disk_cache_read_header(file, &header); + if (!seq_disk_cache_read_header(file, &header)) { + fclose(file); + return NULL; + } int entry_index = seq_disk_cache_get_header_entry(key, &header); /* Item not found. */ |