Skip to content

Commit 8d1eaf1

Browse files
client: Avoid cloning by using is_valid instead
1 parent 7bf989f commit 8d1eaf1

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

client/src/file/api/mod.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,25 @@ impl Keyring {
233233
pub fn remove_items(&mut self, attributes: &impl AsAttributes, key: &Key) -> Result<(), Error> {
234234
let hashed_search = attributes.hash(key);
235235

236-
let (remove, keep): (Vec<EncryptedItem>, _) =
237-
self.items.clone().into_iter().partition(|e| {
238-
hashed_search
239-
.iter()
240-
.all(|(k, v)| v.as_ref().is_ok_and(|v| e.has_attribute(k, v)))
241-
});
242-
243-
// check hashes for the ones to be removed
244-
for item in remove {
245-
item.decrypt(key)?;
236+
// Validate items to be removed before actually removing them
237+
for item in &self.items {
238+
if hashed_search
239+
.iter()
240+
.all(|(k, v)| v.as_ref().is_ok_and(|v| item.has_attribute(k, v)))
241+
{
242+
// Validate by checking if it can be decrypted
243+
if !item.is_valid(key) {
244+
return Err(Error::MacError);
245+
}
246+
}
246247
}
247248

248-
self.items = keep;
249+
// Remove matching items
250+
self.items.retain(|e| {
251+
!hashed_search
252+
.iter()
253+
.all(|(k, v)| v.as_ref().is_ok_and(|v| e.has_attribute(k, v)))
254+
});
249255

250256
Ok(())
251257
}

client/src/file/locked_keyring.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ impl LockedKeyring {
9898
let mut n_broken_items = 0;
9999
let mut n_valid_items = 0;
100100
for encrypted_item in &inner_keyring.items {
101-
if encrypted_item.clone().decrypt(&key).is_err() {
102-
n_broken_items += 1;
103-
} else {
101+
if encrypted_item.is_valid(&key) {
104102
n_valid_items += 1;
103+
} else {
104+
n_broken_items += 1;
105105
}
106106
}
107107

@@ -127,6 +127,7 @@ impl LockedKeyring {
127127
broken_items: n_broken_items,
128128
});
129129
}
130+
130131
Some(Arc::new(key))
131132
} else {
132133
None

0 commit comments

Comments
 (0)