if in doubt, prefer unwrap_or_default()

if the past we had lots of crashes because of unexpected unwrap failures,
mostly related to string.
this commit avoids them eg. for string-conversions that may panic
eg. when encountering a null-byte or by logical programming errors
where an object is assumed to be set but is not under unexpected circumstances.
This commit is contained in:
B. Petersen
2019-10-04 00:05:14 +02:00
parent 93f0f5ccae
commit 477af413c6
28 changed files with 83 additions and 83 deletions

View File

@@ -287,7 +287,7 @@ impl Chat {
if self.typ == Chattype::Group || self.typ == Chattype::VerifiedGroup {
if self.param.get_int(Param::Unpromoted).unwrap_or_default() == 1 {
self.param.remove(Param::Unpromoted);
self.update_param(context).unwrap();
self.update_param(context).unwrap_or_default();
}
}
}
@@ -663,7 +663,7 @@ fn prepare_msg_common(context: &Context, chat_id: u32, msg: &mut Message) -> Res
msg.type_0
);
let mut path_filename = path_filename.unwrap().to_string();
let mut path_filename = path_filename.unwrap_or_default().to_string();
if msg.state == MessageState::OutPreparing && !dc_is_blobdir_path(context, &path_filename) {
bail!("Files must be created in the blob-directory.");
@@ -1367,7 +1367,7 @@ pub(crate) fn add_contact_to_chat_ex(
}
if from_handshake && chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 1 {
chat.param.remove(Param::Unpromoted);
chat.update_param(context).unwrap();
chat.update_param(context).unwrap_or_default();
}
let self_addr = context
.get_config(Config::ConfiguredAddr)
@@ -1502,7 +1502,7 @@ pub fn remove_contact_from_chat(
if chat.is_promoted() {
msg.type_0 = Viewtype::Text;
if contact.id == DC_CONTACT_ID_SELF {
set_group_explicitly_left(context, chat.grpid).unwrap();
set_group_explicitly_left(context, chat.grpid).unwrap_or_default();
msg.text = Some(context.stock_system_msg(
StockMessage::MsgGroupLeft,
"",
@@ -1711,7 +1711,7 @@ pub fn forward_msgs(context: &Context, msg_ids: &[u32], chat_id: u32) -> Result<
let mut created_db_entries = Vec::new();
let mut curr_timestamp: i64;
unarchive(context, chat_id).unwrap();
unarchive(context, chat_id).unwrap_or_default();
if let Ok(mut chat) = Chat::load_from_db(context, chat_id) {
curr_timestamp = dc_create_smeared_timestamps(context, msg_ids.len());
let idsstr = msg_ids
@@ -1732,7 +1732,7 @@ pub fn forward_msgs(context: &Context, msg_ids: &[u32], chat_id: u32) -> Result<
|row| row.get::<_, i32>(0),
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
)
.unwrap(); // TODO: better error handling
.unwrap_or_default(); // TODO: better error handling
for id in ids {
let src_msg_id = id;
@@ -1740,7 +1740,7 @@ pub fn forward_msgs(context: &Context, msg_ids: &[u32], chat_id: u32) -> Result<
if msg.is_err() {
break;
}
let mut msg = msg.unwrap();
let mut msg = msg.unwrap_or_default();
let original_param = msg.param.clone();
if msg.from_id != DC_CONTACT_ID_SELF {
msg.param.set_int(Param::Forwarded, 1);
@@ -1870,7 +1870,7 @@ mod tests {
fn test_get_draft_no_draft() {
let t = dummy_context();
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF).unwrap();
let draft = get_draft(&t.ctx, chat_id).unwrap();
let draft = get_draft(&t.ctx, chat_id).unwrap_or_default();
assert!(draft.is_none());
}