From 4b01846ab0c14dde9a860f1b94311d5b53f397f3 Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Wed, 18 Aug 2021 19:49:05 -0700 Subject: [PATCH] Closes #20901: Do not record viewTime observations when we do not have a set lastAccess The bug here was that we'd try to record `now - 0` as a viewTime delta. This isn't just an obviously wrong value to record, but it will also overflow our storage - we'll end up with a value on disk that doesn't fit into an i32, but HistoryMetadata.total_view_time is i32 in our Rust struct. Once that happens, reads that touch this bad row will result in an overflow and a crash. --- .../fenix/historymetadata/HistoryMetadataService.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/historymetadata/HistoryMetadataService.kt b/app/src/main/java/org/mozilla/fenix/historymetadata/HistoryMetadataService.kt index 6f9df959ae1b..004bbeeed613 100644 --- a/app/src/main/java/org/mozilla/fenix/historymetadata/HistoryMetadataService.kt +++ b/app/src/main/java/org/mozilla/fenix/historymetadata/HistoryMetadataService.kt @@ -81,11 +81,17 @@ class DefaultHistoryMetadataService( } override fun updateMetadata(key: HistoryMetadataKey, tab: TabSessionState) { - logger.debug("Updating metadata for tab $tab") + val lastAccess = tab.lastAccess + if (lastAccess == 0L) { + logger.debug("Not updating metadata for tab $tab - lastAccess=0") + return + } else { + logger.debug("Updating metadata for tab $tab") + } scope.launch { val viewTimeObservation = HistoryMetadataObservation.ViewTimeObservation( - viewTime = (System.currentTimeMillis() - tab.lastAccess).toInt() + viewTime = (System.currentTimeMillis() - lastAccess).toInt() ) storage.noteHistoryMetadataObservation(key, viewTimeObservation) }