Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Document remaining Rust panic guarantees and recover token locks

Manav Rathif0ea15c24eab
diff --git a/rust/apps/cli/src/api/client.rs b/rust/apps/cli/src/api/client.rs
index 1de3628c22..3aa47c0aba 100644
--- a/rust/apps/cli/src/api/client.rs
+++ b/rust/apps/cli/src/api/client.rs
@@ -2,7 +2,7 @@ use crate::models::account::App;
use crate::models::error::Result;
use ente_core::http::{self, Api, ApiConfig, Auth, Http, RetryProfile};
use ente_core::urls::PRODUCTION_API_ORIGIN;
-use std::sync::RwLock;
+use std::sync::{PoisonError, RwLock};
pub(crate) const USER_AGENT: &str = concat!("ente-rs/", env!("CARGO_PKG_VERSION"));
const FILES_ORIGIN: &str = "https://files.ente.com";
@@ -48,11 +48,14 @@ impl AppClient {
.thumbnails
.set_auth(Some(Auth::User(token.to_owned())));
}
- *self.token.write().unwrap() = Some(token.to_owned());
+ *self.token.write().unwrap_or_else(PoisonError::into_inner) = Some(token.to_owned());
}
pub fn token(&self) -> Option<String> {
- self.token.read().unwrap().clone()
+ self.token
+ .read()
+ .unwrap_or_else(PoisonError::into_inner)
+ .clone()
}
pub fn origin(&self) -> &str {
diff --git a/rust/apps/cli/src/sync/download.rs b/rust/apps/cli/src/sync/download.rs
index f8f24069b0..2f1947daba 100644
--- a/rust/apps/cli/src/sync/download.rs
+++ b/rust/apps/cli/src/sync/download.rs
@@ -135,7 +135,7 @@ impl DownloadManager {
pb.set_style(
ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({percent}%) {msg}")
- .unwrap()
+ .expect("valid download progress template")
.progress_chars("#>-"),
);
pb.set_message("Downloading files...");
diff --git a/rust/crates/ensu/src/transcription/text.rs b/rust/crates/ensu/src/transcription/text.rs
index e9ac5adffd..7e15baf3ed 100644
--- a/rust/crates/ensu/src/transcription/text.rs
+++ b/rust/crates/ensu/src/transcription/text.rs
@@ -6,11 +6,15 @@ const FILLER_WORDS: &[&str] = &[
"ehh",
];
-static MULTI_SPACE_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s{2,}").unwrap());
+static MULTI_SPACE_PATTERN: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"\s{2,}").expect("valid whitespace regex"));
static FILLER_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
FILLER_WORDS
.iter()
- .map(|word| Regex::new(&format!(r"(?i)\b{}\b[,.]?", regex::escape(word))).unwrap())
+ .map(|word| {
+ Regex::new(&format!(r"(?i)\b{}\b[,.]?", regex::escape(word)))
+ .expect("escaped filler word forms a valid regex")
+ })
.collect()
});
diff --git a/rust/crates/exif/src/boxes.rs b/rust/crates/exif/src/boxes.rs
index 2259b7f944..db35696fdd 100644
--- a/rust/crates/exif/src/boxes.rs
+++ b/rust/crates/exif/src/boxes.rs
@@ -33,7 +33,7 @@ pub(crate) fn box_at<R: Read + Seek>(
}
range(0, end, offset, size)?;
Ok(BoxRange {
- kind: header[4..8].try_into().unwrap(),
+ kind: header[4..8].try_into().expect("box type is four bytes"),
start: offset + header_size,
len: size - header_size,
})
diff --git a/rust/crates/exif/src/isobmff.rs b/rust/crates/exif/src/isobmff.rs
index eb7c607b4f..0f6e3d1930 100644
--- a/rust/crates/exif/src/isobmff.rs
+++ b/rust/crates/exif/src/isobmff.rs
@@ -289,7 +289,7 @@ fn item_info<R: Read + Seek>(
}
let id = c.number(if version == 2 { 2 } else { 4 })? as u32;
let protection = c.number(2)?;
- let kind: [u8; 4] = c.take(4)?.try_into().unwrap();
+ let kind: [u8; 4] = c.take(4)?.try_into().expect("item type is four bytes");
c.string()?;
let mime = if &kind == b"mime" {
c.string()?.to_owned()
diff --git a/rust/crates/ml/src/vecdb/log.rs b/rust/crates/ml/src/vecdb/log.rs
index e0ba968df7..f4f1e96150 100644
--- a/rust/crates/ml/src/vecdb/log.rs
+++ b/rust/crates/ml/src/vecdb/log.rs
@@ -399,7 +399,10 @@ impl LogScanner<'_> {
}
fn read_attr_bytes(&mut self, body_budget: u64) -> Result<bool, VecDbError> {
- let attr_count = *self.scratch.last().unwrap();
+ let attr_count = *self
+ .scratch
+ .last()
+ .expect("add record includes an attribute count");
if attr_count as usize > MAX_ATTR_COUNT {
return Ok(false);
}
@@ -408,14 +411,14 @@ impl LogScanner<'_> {
if !self.extend_within(body_budget, 1)? {
return Ok(false);
}
- let name_len = *self.scratch.last().unwrap() as usize;
+ let name_len = *self.scratch.last().expect("attribute name length was read") as usize;
if name_len == 0 || name_len > MAX_ATTR_NAME_BYTES {
return Ok(false);
}
if !self.extend_within(body_budget, name_len + 1)? {
return Ok(false);
}
- let value_len = match *self.scratch.last().unwrap() {
+ let value_len = match *self.scratch.last().expect("attribute value tag was read") {
ATTR_TAG_STR => {
if !self.extend_within(body_budget, 2)? {
return Ok(false);
diff --git a/rust/crates/photos/src/db.rs b/rust/crates/photos/src/db.rs
index 82c1537847..a39d6fcd04 100644
--- a/rust/crates/photos/src/db.rs
+++ b/rust/crates/photos/src/db.rs
@@ -303,7 +303,12 @@ impl Pool {
fn read<T>(&self, query: impl FnOnce(&mut Connection) -> Result<T>) -> Result<T> {
let mut reader = self.acquire_reader();
- query(reader.connection.as_mut().unwrap())
+ query(
+ reader
+ .connection
+ .as_mut()
+ .expect("acquired reader owns a connection"),
+ )
}
fn write<T>(&self, statement: impl FnOnce(&mut Connection) -> Result<T>) -> Result<T> {