Skip to content

Commit

Permalink
[bettereng] Fold a bunch of panics into the Error case of their Result
Browse files Browse the repository at this point in the history
Note one case in the github secure storage is a panic occurring in
case an invalid UTF8 encoding is parsed anywhere in the storage, which
does not bode well.
  • Loading branch information
huitseeker authored and bors-libra committed Aug 26, 2020
1 parent 69fd4f4 commit d43a16d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion language/move-lang/src/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl Address {
hex_string.insert(0, '0');
}

let mut result = hex::decode(hex_string.as_str()).unwrap();
let mut result = hex::decode(hex_string.as_str())
.map_err(|_| "hex string does not decode properly".to_owned())?;
let len = result.len();
if len < ADDRESS_LENGTH {
result.reverse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ impl<'a> BorrowAnalysis<'a> {
let livevar_annotation_at = self
.livevar_annotation
.get_live_var_info_at(code_offset)
.unwrap();
.ok_or_else(|| PackError {
code_offset,
indices,
})?;
for idx in livevar_annotation_at
.before
.difference(&livevar_annotation_at.after)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'a> CopyAnalysis<'a> {
let after_refs = &self
.borrow_annotation
.get_borrow_info_at(code_offset)
.unwrap()
.ok_or_else(|| ())?
.after
.all_refs();
post.copies = post.copies.intersection(after_refs).cloned().collect();
Expand Down
4 changes: 3 additions & 1 deletion secure/storage/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ impl KVStorage for GitHubStorage {
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<GetResponse<T>, Error> {
let data = self.client.get_file(key)?;
let data = base64::decode(&data)?;
let data = std::str::from_utf8(&data).unwrap();
let data = std::str::from_utf8(&data).map_err(|_| {
Error::InternalError("Unparseable data returned from Github KV Storage".into())
})?;
serde_json::from_str(&data).map_err(|e| e.into())
}

Expand Down
4 changes: 3 additions & 1 deletion testsuite/cli/libra-wallet/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl Mnemonic {
bit_writer.write_buffer();

// This will never fail as we've already checked the word-list is not empty.
let (checksum, entropy) = bit_writer.bytes.split_last().unwrap();
let (checksum, entropy) = bit_writer.bytes.split_last().ok_or_else(|| {
WalletError::LibraWalletGeneric("Bit writer producing inconsistent byte strings".into())
})?;
let computed_checksum = Sha256::digest(entropy)[0] >> (8 - len / 3);
// Checksum validation.
if *checksum != computed_checksum {
Expand Down

0 comments on commit d43a16d

Please sign in to comment.