refactor: safe sql access

This commit is contained in:
dignifiedquire
2019-06-08 16:42:29 +02:00
parent 205493f89d
commit ab41679855
34 changed files with 6550 additions and 8249 deletions

View File

@@ -4,7 +4,6 @@ use crate::constants::*;
use crate::context::Context;
use crate::dc_sqlite3::*;
use crate::key::*;
use crate::types::*;
#[derive(Default, Clone, Debug)]
pub struct Keyring<'a> {
@@ -31,29 +30,18 @@ impl<'a> Keyring<'a> {
pub fn load_self_private_for_decrypting(
&mut self,
context: &Context,
self_addr: *const libc::c_char,
self_addr: impl AsRef<str>,
sql: &SQLite,
) -> bool {
// Can we prevent keyring and self_addr to be null?
if self_addr.is_null() {
return false;
}
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;\x00"
as *const u8 as *const libc::c_char,
)
};
unsafe { sqlite3_bind_text(stmt, 1, self_addr, -1, None) };
while unsafe { sqlite3_step(stmt) == 100 } {
if let Some(key) = Key::from_stmt(stmt, 0, KeyType::Private) {
self.add_owned(key);
}
}
unsafe { sqlite3_finalize(stmt) };
true
dc_sqlite3_query_row(
context,
sql,
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
&[self_addr.as_ref()],
0,
)
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Private))
.map(|key| self.add_owned(key))
.is_some()
}
}