From 966bb2271aaca0f622960f8311fafb9f9954dce3 Mon Sep 17 00:00:00 2001 From: jikstra Date: Sat, 10 Aug 2019 16:47:38 +0200 Subject: [PATCH 1/6] Put something into the msg object if we fail to get a valid string out of the db --- src/dc_chat.rs | 1 + src/dc_msg.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 5497ad927..14b6a73de 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -1081,6 +1081,7 @@ pub fn dc_get_chat_msgs( let process_rows = |rows: rusqlite::MappedRows<_>| { for row in rows { let (curr_id, ts) = row?; + //println!("{}", curr_id); if curr_id as u32 == marker1before { ret.add_id(1); } diff --git a/src/dc_msg.rs b/src/dc_msg.rs index 963cb6ed4..c86a3cb7a 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -436,8 +436,12 @@ pub unsafe fn dc_msg_get_timestamp(msg: *const dc_msg_t) -> i64 { }; } +//pub fn dc_msg_load_from_db_r(context: &'a Context, id: u32) -> Result { +//} + pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: u32) -> bool { if msg.is_null() { + println!("xxx msg is null {}", id); return false; } @@ -472,7 +476,15 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - (*msg).text = row.get::<_, Option>(15)?; + println!("Before2 text: {}", id); + (*msg).text = match row.get(15) { + Ok(text) => Some(text), + Err(e) => { + warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {} because of {}", id, e); + Some("[ Could not read from db ]".to_string()) + } + }; + println!("After text: {}", id); (*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default(); (*msg).starred = row.get(17)?; (*msg).hidden = row.get(18)?; @@ -491,8 +503,12 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: Ok(()) } }); - - res.is_ok() + // HERE WE ARE! + if let Err(e) = res { + warn!(context, 0, "Error in msg_load_from_db for id {} because of {}", id, e); + return false; + } + true } pub unsafe fn dc_get_mime_headers(context: &Context, msg_id: uint32_t) -> *mut libc::c_char { @@ -628,14 +644,17 @@ pub fn dc_star_msgs( } pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_msg_t<'a> { - let mut success: libc::c_int = 0i32; + let mut success = false; let obj: *mut dc_msg_t = dc_msg_new_untyped(context); if dc_msg_load_from_db(obj, context, msg_id) { - success = 1i32 + success = true } - if 0 != success { + + + if success { obj } else { + println!("No success for {}", msg_id); dc_msg_unref(obj); 0 as *mut dc_msg_t } From b23c4b4da6b9827e301a20370c0791d14ba2f6e2 Mon Sep 17 00:00:00 2001 From: jikstra Date: Sat, 10 Aug 2019 16:51:57 +0200 Subject: [PATCH 2/6] Remove debug printlns, refactor a bit --- src/dc_chat.rs | 1 - src/dc_msg.rs | 21 ++++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 14b6a73de..5497ad927 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -1081,7 +1081,6 @@ pub fn dc_get_chat_msgs( let process_rows = |rows: rusqlite::MappedRows<_>| { for row in rows { let (curr_id, ts) = row?; - //println!("{}", curr_id); if curr_id as u32 == marker1before { ret.add_id(1); } diff --git a/src/dc_msg.rs b/src/dc_msg.rs index c86a3cb7a..586092375 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -441,7 +441,6 @@ pub unsafe fn dc_msg_get_timestamp(msg: *const dc_msg_t) -> i64 { pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: u32) -> bool { if msg.is_null() { - println!("xxx msg is null {}", id); return false; } @@ -476,15 +475,13 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - println!("Before2 text: {}", id); - (*msg).text = match row.get(15) { - Ok(text) => Some(text), + (*msg).text = Some(match row.get(15) { + Ok(text) => text, Err(e) => { warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {} because of {}", id, e); - Some("[ Could not read from db ]".to_string()) + "[ Could not read from db ]".to_string() } - }; - println!("After text: {}", id); + }); (*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default(); (*msg).starred = row.get(17)?; (*msg).hidden = row.get(18)?; @@ -503,10 +500,13 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: Ok(()) } }); - // HERE WE ARE! + if let Err(e) = res { - warn!(context, 0, "Error in msg_load_from_db for id {} because of {}", id, e); - return false; + warn!( + context, + 0, "Error in msg_load_from_db for id {} because of {}", id, e + ); + return false; } true } @@ -650,7 +650,6 @@ pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_ success = true } - if success { obj } else { From faf53fe11e5daa12f80d8482b42222e9ffea0627 Mon Sep 17 00:00:00 2001 From: jikstra Date: Sat, 10 Aug 2019 17:53:05 +0200 Subject: [PATCH 3/6] Manually get a lossy utf8 string from the database if other fails --- src/dc_msg.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/dc_msg.rs b/src/dc_msg.rs index 586092375..180683c65 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -436,9 +436,6 @@ pub unsafe fn dc_msg_get_timestamp(msg: *const dc_msg_t) -> i64 { }; } -//pub fn dc_msg_load_from_db_r(context: &'a Context, id: u32) -> Result { -//} - pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: u32) -> bool { if msg.is_null() { return false; @@ -475,13 +472,19 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - (*msg).text = Some(match row.get(15) { - Ok(text) => text, - Err(e) => { - warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {} because of {}", id, e); - "[ Could not read from db ]".to_string() + let text; + if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { + if let Ok(t) = String::from_utf8(buf.to_vec()) { + text = t; + } else { + warn!(context, 0, "dc_msg_load_from_db: could not get text column as non-lossy utf8 id {}", id); + text = String::from_utf8_lossy(buf).into_owned(); } - }); + } else { + warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {}", id); + text = "[ Could not read from db ]".to_string() + } + (*msg).text = Some(text); (*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default(); (*msg).starred = row.get(17)?; (*msg).hidden = row.get(18)?; @@ -497,7 +500,7 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: free(ptr.cast()); } }; - Ok(()) + Ok(()) } }); From b7ff996b1537155fcde20075c84ba562e0aa5655 Mon Sep 17 00:00:00 2001 From: jikstra Date: Sat, 10 Aug 2019 17:57:53 +0200 Subject: [PATCH 4/6] Cargo fmt + refactoring --- src/dc_msg.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/dc_msg.rs b/src/dc_msg.rs index 180683c65..acff93cb1 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -472,19 +472,17 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - let text; - if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { + (*msg).text = Some(if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { if let Ok(t) = String::from_utf8(buf.to_vec()) { - text = t; + t } else { warn!(context, 0, "dc_msg_load_from_db: could not get text column as non-lossy utf8 id {}", id); - text = String::from_utf8_lossy(buf).into_owned(); + String::from_utf8_lossy(buf).into_owned() } } else { warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {}", id); - text = "[ Could not read from db ]".to_string() - } - (*msg).text = Some(text); + "[ Could not read from db ]".to_string() + }); (*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default(); (*msg).starred = row.get(17)?; (*msg).hidden = row.get(18)?; From 3ba847ece2cb303a55eb6ca3832e5484377861ac Mon Sep 17 00:00:00 2001 From: jikstra Date: Sun, 11 Aug 2019 16:57:49 +0200 Subject: [PATCH 5/6] Apply requested changes --- src/dc_msg.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/dc_msg.rs b/src/dc_msg.rs index acff93cb1..f0072083d 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -472,17 +472,21 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - (*msg).text = Some(if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { + + let text; + if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { if let Ok(t) = String::from_utf8(buf.to_vec()) { - t + text = t; } else { warn!(context, 0, "dc_msg_load_from_db: could not get text column as non-lossy utf8 id {}", id); - String::from_utf8_lossy(buf).into_owned() + text = String::from_utf8_lossy(buf).into_owned(); } } else { warn!(context, 0, "dc_msg_load_from_db: could not get text column for id {}", id); - "[ Could not read from db ]".to_string() - }); + text = "[ Could not read from db ]".to_string(); + } + (*msg).text = Some(text); + (*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default(); (*msg).starred = row.get(17)?; (*msg).hidden = row.get(18)?; @@ -654,7 +658,6 @@ pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_ if success { obj } else { - println!("No success for {}", msg_id); dc_msg_unref(obj); 0 as *mut dc_msg_t } From a3683be0476d3bda03bfc89bc50870da2245767b Mon Sep 17 00:00:00 2001 From: jikstra Date: Sun, 11 Aug 2019 18:55:23 +0200 Subject: [PATCH 6/6] cargo fmt --- src/dc_msg.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dc_msg.rs b/src/dc_msg.rs index f0072083d..aa53187ab 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -472,7 +472,7 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: (*msg).type_0 = row.get(12)?; (*msg).state = row.get(13)?; (*msg).is_dc_message = row.get(14)?; - + let text; if let rusqlite::types::ValueRef::Text(buf) = row.get_raw(15) { if let Ok(t) = String::from_utf8(buf.to_vec()) {