From f3364cfdc0220d807d8092e22e38df6487e93c1a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 13 May 2021 09:51:57 +0530 Subject: [PATCH] Guard against invalid cache file in a couple more places --- kitty/disk-cache.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/kitty/disk-cache.c b/kitty/disk-cache.c index 95452fa0d..3915a375e 100644 --- a/kitty/disk-cache.c +++ b/kitty/disk-cache.c @@ -165,8 +165,11 @@ size_of_cache_file(DiskCache *self) { size_t disk_cache_size_on_disk(PyObject *self) { - off_t ans = size_of_cache_file((DiskCache*)self); - return MAX(0, ans); + if (((DiskCache*)self)->cache_file_fd > -1) { + off_t ans = size_of_cache_file((DiskCache*)self); + return MAX(0, ans); + } + return 0; } typedef struct { @@ -362,7 +365,9 @@ write_loop(void *data) { continue; } else if (!count) { mutex(lock); - if (ftruncate(self->cache_file_fd, 0) == 0) lseek(self->cache_file_fd, 0, SEEK_END); + if (self->cache_file_fd > -1) { + if (ftruncate(self->cache_file_fd, 0) == 0) lseek(self->cache_file_fd, 0, SEEK_END); + } mutex(unlock); }