From: Timo Sirainen Date: Mon, 10 Feb 2020 15:44:29 +0000 (+0200) Subject: lib-index: Add mail_index_cache_optimization_settings.max_size X-Git-Tag: 2.3.11.2~609 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35775f011b25b35286cbe39cfe5bf1bc4ca393ad;p=thirdparty%2Fdovecot%2Fcore.git lib-index: Add mail_index_cache_optimization_settings.max_size This can be used to limit the cache file's maximum size. --- diff --git a/src/lib-index/mail-cache-transaction.c b/src/lib-index/mail-cache-transaction.c index d09a47a9d2..0ff2545c4a 100644 --- a/src/lib-index/mail-cache-transaction.c +++ b/src/lib-index/mail-cache-transaction.c @@ -436,7 +436,7 @@ mail_cache_transaction_flush(struct mail_cache_transaction_ctx *ctx) if (!ESTALE_FSTAT(errno)) mail_cache_set_syscall_error(ctx->cache, "fstat()"); ret = -1; - } else if ((uint32_t)-1 < st.st_size + ctx->last_rec_pos) { + } else if (st.st_size + ctx->last_rec_pos > ctx->cache->index->optimization_set.cache.max_size) { mail_cache_set_corrupted(ctx->cache, "Cache file too large"); ret = -1; } else { diff --git a/src/lib-index/mail-cache.c b/src/lib-index/mail-cache.c index 8f4f2e57d0..caa3d12563 100644 --- a/src/lib-index/mail-cache.c +++ b/src/lib-index/mail-cache.c @@ -796,13 +796,14 @@ int mail_cache_append(struct mail_cache *cache, const void *data, size_t size, mail_cache_set_syscall_error(cache, "fstat()"); return -1; } - if (st.st_size > (uint32_t)-1) { + if ((uoff_t)st.st_size > cache->index->optimization_set.cache.max_size) { mail_cache_set_corrupted(cache, "Cache file too large"); return -1; } *offset = st.st_size; } - if ((uint32_t)-1 - *offset < size) { + if (*offset >= cache->index->optimization_set.cache.max_size || + cache->index->optimization_set.cache.max_size - *offset < size) { mail_cache_set_corrupted(cache, "Cache file too large"); return -1; } diff --git a/src/lib-index/mail-index.c b/src/lib-index/mail-index.c index 0daf8cf9ca..8f342a40e1 100644 --- a/src/lib-index/mail-index.c +++ b/src/lib-index/mail-index.c @@ -47,6 +47,7 @@ static const struct mail_index_optimization_settings default_optimization_set = .cache = { .unaccessed_field_drop_secs = 3600 * 24 * 30, .record_max_size = 64 * 1024, + .max_size = (uint32_t)-1, .compress_min_size = 32 * 1024, .compress_delete_percentage = 20, .compress_continued_percentage = 200, @@ -210,6 +211,8 @@ void mail_index_set_optimization_settings(struct mail_index *index, if (set->cache.unaccessed_field_drop_secs != 0) dest->cache.unaccessed_field_drop_secs = set->cache.unaccessed_field_drop_secs; + if (set->cache.max_size != 0) + dest->cache.max_size = set->cache.max_size; if (set->cache.compress_min_size != 0) dest->cache.compress_min_size = set->cache.compress_min_size; if (set->cache.compress_delete_percentage != 0) diff --git a/src/lib-index/mail-index.h b/src/lib-index/mail-index.h index a54c41d9f6..169e32f6b2 100644 --- a/src/lib-index/mail-index.h +++ b/src/lib-index/mail-index.h @@ -274,6 +274,8 @@ struct mail_index_cache_optimization_settings { /* If cache record becomes larger than this, don't add it. */ unsigned int record_max_size; + /* Maximum size for the cache file. Internally the limit is 1 GB. */ + uoff_t max_size; /* Never compress the file if it's smaller than this */ uoff_t compress_min_size; /* Compress the file when n% of records are deleted */