@@ -279,10 +279,20 @@ impl EventCacheStore for SqliteEventCacheStore {
279
279
280
280
// Finally, if the cache size is too big, remove old items until it fits.
281
281
if let Some ( max_cache_size) = policy. max_cache_size {
282
- let cache_size: usize =
283
- txn. query_row ( "SELECT sum(length(data)) FROM media" , ( ) , |row| row. get ( 0 ) ) ?;
284
-
285
- if cache_size > max_cache_size {
282
+ // i64 is the integer type used by SQLite, use it here to avoid usize overflow
283
+ // during the conversion of the result.
284
+ let cache_size_int = txn
285
+ . query_row ( "SELECT sum(length(data)) FROM media" , ( ) , |row| {
286
+ // `sum()` returns `NULL` if there are no rows.
287
+ row. get :: < _ , Option < i64 > > ( 0 )
288
+ } ) ?
289
+ . unwrap_or_default ( ) ;
290
+ let cache_size_usize = usize:: try_from ( cache_size_int) ;
291
+
292
+ // If the cache size is overflowing or bigger than max cache size, clean up.
293
+ if cache_size_usize. is_err ( )
294
+ || cache_size_usize. is_ok_and ( |cache_size| cache_size > max_cache_size)
295
+ {
286
296
// Get the sizes of the media contents ordered by last access.
287
297
let mut cached_stmt = txn. prepare_cached (
288
298
"SELECT rowid, length(data) FROM media ORDER BY last_access DESC" ,
0 commit comments