From 7f3729d0d8f57b87f1faa17fb67548e553bd5eb1 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez Date: Fri, 21 Jun 2024 02:09:21 -0500 Subject: [PATCH] =?UTF-8?q?=20Bug=201893406=20-=20Always=20provide=20a=20v?= =?UTF-8?q?alid=20installation=20time=20in=20WER=20crash=20r=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …eports a=RyanVM Original Revision: https://phabricator.services.mozilla.com/D209234 Differential Revision: https://phabricator.services.mozilla.com/D211825 --- toolkit/crashreporter/mozwer-rust/lib.rs | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/toolkit/crashreporter/mozwer-rust/lib.rs b/toolkit/crashreporter/mozwer-rust/lib.rs index 58d183da319..6888abe291c 100644 --- a/toolkit/crashreporter/mozwer-rust/lib.rs +++ b/toolkit/crashreporter/mozwer-rust/lib.rs @@ -12,7 +12,7 @@ use serde_json::ser::to_writer; use std::convert::TryInto; use std::ffi::OsString; - use std::fs::{read_to_string, DirBuilder, File}; + use std::fs::{read_to_string, DirBuilder, File, OpenOptions}; use std::io::{BufRead, BufReader, Write}; use std::mem::{size_of, transmute, zeroed}; use std::os::windows::ffi::{OsStrExt, OsStringExt}; @@ -442,8 +442,7 @@ let install_time = ApplicationInformation::get_install_time( &crash_reports_dir, &application_data.build_id, - ) - .unwrap_or("0".to_string()); + ); Ok(ApplicationInformation { install_path, @@ -514,10 +513,29 @@ } } - fn get_install_time(crash_reports_path: &Path, build_id: &str) -> Result { + fn get_install_time(crash_reports_path: &Path, build_id: &str) -> String { let file_name = "InstallTime".to_owned() + build_id; let file_path = crash_reports_path.join(file_name); - read_to_string(file_path).map_err(|_e| ()) + + // If the file isn't present we'll attempt to atomically create it and + // populate it. This code essentially matches the corresponding code in + // nsExceptionHandler.cpp SetupExtraData(). + if let Ok(mut file) = OpenOptions::new() + .create_new(true) + .write(true) + .open(&file_path) + { + // SAFETY: No risks in calling `time()` with a null pointer. + let _ = write!(&mut file, "{}", unsafe { time(null_mut()) }.to_string()); + } + + // As a last resort, if we can't read the file we fall back to the + // current time. This might cause us to overstate the number of users + // affected by a crash, but given it's very unlikely to hit this particular + // path it won't be a problem. + // + // SAFETY: No risks in calling `time()` with a null pointer. + read_to_string(&file_path).unwrap_or(unsafe { time(null_mut()) }.to_string()) } }