Guard against invalid cache file in a couple more places

This commit is contained in:
Kovid Goyal 2021-05-13 09:51:57 +05:30
parent f64b4e0e56
commit f3364cfdc0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -165,8 +165,11 @@ size_of_cache_file(DiskCache *self) {
size_t size_t
disk_cache_size_on_disk(PyObject *self) { disk_cache_size_on_disk(PyObject *self) {
off_t ans = size_of_cache_file((DiskCache*)self); if (((DiskCache*)self)->cache_file_fd > -1) {
return MAX(0, ans); off_t ans = size_of_cache_file((DiskCache*)self);
return MAX(0, ans);
}
return 0;
} }
typedef struct { typedef struct {
@ -362,7 +365,9 @@ write_loop(void *data) {
continue; continue;
} else if (!count) { } else if (!count) {
mutex(lock); 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); mutex(unlock);
} }