Fix crash if sync DeviceInfo pref contains corrupt entries

DeviceInfoPrefs should not crash during garbage collection even if the
local preference contains unexpected (corrupt) data, for example if an
external tool mangled with preferences.

Change-Id: I8c13770357ea150bbb8c93c721bb17721ce15d4b
Bug: 1029673
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2160898
Reviewed-by: Marc Treib <[email protected]>
Commit-Queue: Mikel Astiz <[email protected]>
Cr-Commit-Position: refs/heads/master@{#761866}
diff --git a/components/sync_device_info/device_info_prefs.cc b/components/sync_device_info/device_info_prefs.cc
index 2f47a346..5f02f05 100644
--- a/components/sync_device_info/device_info_prefs.cc
+++ b/components/sync_device_info/device_info_prefs.cc
@@ -130,11 +130,22 @@
   ListPrefUpdate update_cache_guids(pref_service_,
                                     kDeviceInfoRecentGUIDsWithTimestamps);
   update_cache_guids->EraseListValueIf([this](const auto& dict) {
+    // Avoid crashes if the preference contains corrupt entries that are not
+    // dictionaries, and meanwhile clean up these corrupt entries.
+    if (!dict.is_dict()) {
+      return true;
+    }
+
     base::Optional<int> days_since_epoch = dict.FindIntKey(kTimestampKey);
-    const base::Time creation_time =
-        days_since_epoch ? base::Time::FromDeltaSinceWindowsEpoch(
-                               base::TimeDelta::FromDays(*days_since_epoch))
-                         : base::Time::Min();
+
+    // Avoid crashes if the dictionary contains no timestamp and meanwhile clean
+    // up these corrupt entries.
+    if (!days_since_epoch.has_value()) {
+      return true;
+    }
+
+    const base::Time creation_time = base::Time::FromDeltaSinceWindowsEpoch(
+        base::TimeDelta::FromDays(*days_since_epoch));
     return creation_time < clock_->Now() - kMaxTimeDeltaLocalCacheGuidsStored;
   });
 }