Skip to content

change type of dc_msg_t.text to String #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub unsafe extern "C" fn dc_send_text_msg(
) -> u32 {
assert!(!context.is_null());
let context = &*context;
let text_to_send = dc_tools::to_string_lossy(text_to_send);

dc_chat::dc_send_text_msg(context, chat_id, text_to_send)
}
Expand Down
19 changes: 3 additions & 16 deletions examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,30 +914,17 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(!sel_chat.is_null(), "No chat selected.");
ensure!(!arg1.is_empty(), "No message text given.");

let msg = CString::yolo(format!("{} {}", arg1, arg2));
let msg = format!("{} {}", arg1, arg2);

if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg.as_ptr()) {
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg) {
println!("Message sent.");
} else {
bail!("Sending failed.");
}
}
"send-garbage" => {
ensure!(!sel_chat.is_null(), "No chat selected.");
let msg = b"\xff\x00"; // NUL-terminated C-string, that is malformed utf-8
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg.as_ptr().cast()) {
println!("Malformed utf-8 succesfully send. Not nice.");
} else {
bail!("Garbage sending failed, as expected.");
}
}
"sendempty" => {
ensure!(!sel_chat.is_null(), "No chat selected.");
if 0 != dc_send_text_msg(
context,
dc_chat_get_id(sel_chat),
b"\x00" as *const u8 as *const libc::c_char,
) {
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), "".into()) {
println!("Message sent.");
} else {
bail!("Sending failed.");
Expand Down
3 changes: 1 addition & 2 deletions examples/repl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ const DB_COMMANDS: [&'static str; 11] = [
"housekeeping",
];

const CHAT_COMMANDS: [&'static str; 25] = [
const CHAT_COMMANDS: [&'static str; 24] = [
"listchats",
"listarchived",
"chat",
Expand All @@ -309,7 +309,6 @@ const CHAT_COMMANDS: [&'static str; 25] = [
"dellocations",
"getlocations",
"send",
"send-garbage",
"sendimage",
"sendfile",
"draft",
Expand Down
3 changes: 1 addition & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ fn main() {
println!("sending a message");
let contact_id = dc_create_contact(&ctx, std::ptr::null(), email.as_ptr());
let chat_id = dc_create_chat_by_contact_id(&ctx, contact_id);
let msg_text = CString::new("Hi, here is my first message!").unwrap();
dc_send_text_msg(&ctx, chat_id, msg_text.as_ptr());
dc_send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into());

println!("fetching chats..");
let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap();
Expand Down
104 changes: 39 additions & 65 deletions src/dc_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ unsafe fn prepare_msg_raw(
timestamp,
(*msg).type_0,
(*msg).state,
if !(*msg).text.is_null() { Some(as_str((*msg).text)) } else { None },
(*msg).text,
(*msg).param.to_string(),
(*msg).hidden,
to_string(new_in_reply_to),
Expand Down Expand Up @@ -950,7 +950,7 @@ pub unsafe fn dc_send_msg<'a>(
pub unsafe fn dc_send_text_msg(
context: &Context,
chat_id: uint32_t,
text_to_send: *const libc::c_char,
text_to_send: String,
) -> uint32_t {
if chat_id <= 9 {
warn!(
Expand All @@ -960,18 +960,8 @@ pub unsafe fn dc_send_text_msg(
return 0;
}

if text_to_send.is_null() {
warn!(context, 0, "dc_send_text_msg: text_to_send is emtpy");
return 0;
}

if let Err(err) = as_str_safe(text_to_send) {
warn!(context, 0, "{}", err);
return 0;
}

let mut msg = dc_msg_new(context, Viewtype::Text);
(*msg).text = dc_strdup(text_to_send);
(*msg).text = Some(text_to_send);
let ret = dc_send_msg(context, chat_id, msg);
dc_msg_unref(msg);
ret
Expand Down Expand Up @@ -1001,9 +991,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
// save new draft
if !msg.is_null() {
if (*msg).type_0 == Viewtype::Text {
if (*msg).text.is_null() || *(*msg).text.offset(0isize) as libc::c_int == 0i32 {
OK_TO_CONTINUE = false;
}
OK_TO_CONTINUE = (*msg).text.as_ref().map_or(false, |s| !s.is_empty());
} else if msgtype_has_file((*msg).type_0) {
let mut pathNfilename = (*msg)
.param
Expand Down Expand Up @@ -1036,11 +1024,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
time(),
(*msg).type_0,
DC_STATE_OUT_DRAFT,
if !(*msg).text.is_null() {
as_str((*msg).text)
} else {
""
},
(*msg).text.as_ref().map(String::as_str).unwrap_or(""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.unwrap_or_default() would be the same? Not that I mind.

(*msg).param.to_string(),
1,
],
Expand Down Expand Up @@ -1616,14 +1600,12 @@ pub unsafe fn dc_add_contact_to_chat_ex(
if OK_TO_CONTINUE {
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).type_0 = Viewtype::Text;
(*msg).text = context
.stock_system_msg(
StockMessage::MsgAddMember,
as_str((*contact).addr),
"",
DC_CONTACT_ID_SELF as uint32_t,
)
.strdup();
(*msg).text = Some(context.stock_system_msg(
StockMessage::MsgAddMember,
as_str((*contact).addr),
"",
DC_CONTACT_ID_SELF as uint32_t,
));
(*msg).param.set_int(Param::Cmd, 4);
if !(*contact).addr.is_null() {
(*msg).param.set(Param::Arg, as_str((*contact).addr));
Expand Down Expand Up @@ -1731,23 +1713,19 @@ pub unsafe fn dc_remove_contact_from_chat(
(*msg).type_0 = Viewtype::Text;
if (*contact).id == 1 as libc::c_uint {
dc_set_group_explicitly_left(context, (*chat).grpid);
(*msg).text = context
.stock_system_msg(
StockMessage::MsgGroupLeft,
"",
"",
DC_CONTACT_ID_SELF as u32,
)
.strdup();
(*msg).text = Some(context.stock_system_msg(
StockMessage::MsgGroupLeft,
"",
"",
DC_CONTACT_ID_SELF as u32,
));
} else {
(*msg).text = context
.stock_system_msg(
StockMessage::MsgDelMember,
as_str((*contact).addr),
"",
DC_CONTACT_ID_SELF as u32,
)
.strdup();
(*msg).text = Some(context.stock_system_msg(
StockMessage::MsgDelMember,
as_str((*contact).addr),
"",
DC_CONTACT_ID_SELF as u32,
));
}
(*msg).param.set_int(Param::Cmd, 5);
if !(*contact).addr.is_null() {
Expand Down Expand Up @@ -1848,14 +1826,12 @@ pub unsafe fn dc_set_chat_name(
{
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).type_0 = Viewtype::Text;
(*msg).text = context
.stock_system_msg(
StockMessage::MsgGrpName,
as_str((*chat).name),
as_str(new_name),
DC_CONTACT_ID_SELF as u32,
)
.strdup();
(*msg).text = Some(context.stock_system_msg(
StockMessage::MsgGrpName,
as_str((*chat).name),
as_str(new_name),
DC_CONTACT_ID_SELF as u32,
));
(*msg).param.set_int(Param::Cmd, 2);
if !(*chat).name.is_null() {
(*msg).param.set(Param::Arg, as_str((*chat).name));
Expand Down Expand Up @@ -1922,18 +1898,16 @@ pub unsafe fn dc_set_chat_profile_image(
(*msg).param.set_int(Param::Cmd, 3);
(*msg).param.set(Param::Arg, as_str(new_image_rel));
(*msg).type_0 = Viewtype::Text;
(*msg).text = context
.stock_system_msg(
if !new_image_rel.is_null() {
StockMessage::MsgGrpImgChanged
} else {
StockMessage::MsgGrpImgDeleted
},
"",
"",
DC_CONTACT_ID_SELF as uint32_t,
)
.strdup();
(*msg).text = Some(context.stock_system_msg(
if !new_image_rel.is_null() {
StockMessage::MsgGrpImgChanged
} else {
StockMessage::MsgGrpImgDeleted
},
"",
"",
DC_CONTACT_ID_SELF as uint32_t,
));
(*msg).id = dc_send_msg(context, chat_id, msg);
context.call_cb(
Event::MSGS_CHANGED,
Expand Down
5 changes: 2 additions & 3 deletions src/dc_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ pub unsafe fn dc_send_locations_to_chat(
{
if 0 != seconds && !is_sending_locations_before {
msg = dc_msg_new(context, Viewtype::Text);
(*msg).text = context
.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0)
.strdup();
(*msg).text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
(*msg).param.set_int(Param::Cmd, 8);
dc_send_msg(context, chat_id, msg);
} else if 0 == seconds && is_sending_locations_before {
Expand Down
12 changes: 11 additions & 1 deletion src/dc_lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::dc_tools::*;
use crate::stock::StockMessage;
use crate::types::*;
use crate::x::*;
use std::ffi::CString;
use std::ptr;

/* * Structure behind dc_lot_t */
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -160,8 +162,16 @@ pub unsafe fn dc_lot_fill(
(*lot).text1_meaning = 2i32
}
}

let msgtext_c = (*msg)
.text
.as_ref()
.map(|s| CString::yolo(String::as_str(s)));
let msgtext_ptr = msgtext_c.map_or(ptr::null(), |s| s.as_ptr());

(*lot).text2 =
dc_msg_get_summarytext_by_raw((*msg).type_0, (*msg).text, &mut (*msg).param, 160, context);
dc_msg_get_summarytext_by_raw((*msg).type_0, msgtext_ptr, &mut (*msg).param, 160, context);

(*lot).timestamp = dc_msg_get_timestamp(msg);
(*lot).state = (*msg).state;
}
39 changes: 25 additions & 14 deletions src/dc_mimefactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use mmime::mailmime_types_helper::*;
use mmime::mailmime_write_mem::*;
use mmime::mmapstring::*;
use mmime::other::*;
use std::ptr;

use crate::constants::*;
use crate::context::Context;
Expand Down Expand Up @@ -799,12 +800,17 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
)
}

let mut final_text: *const libc::c_char = 0 as *const libc::c_char;
if !placeholdertext.is_null() {
final_text = placeholdertext
} else if !(*msg).text.is_null() && 0 != *(*msg).text.offset(0isize) as libc::c_int {
final_text = (*msg).text
}
let final_text = {
if !placeholdertext.is_null() {
to_string(placeholdertext)
} else if let Some(ref text) = (*msg).text {
text.clone()
} else {
"".into()
}
};
let final_text = CString::yolo(final_text);

let footer: *mut libc::c_char = (*factory).selfstatus;
message_text = dc_mprintf(
b"%s%s%s%s%s\x00" as *const u8 as *const libc::c_char,
Expand All @@ -813,12 +819,8 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
} else {
b"\x00" as *const u8 as *const libc::c_char
},
if !final_text.is_null() {
final_text
} else {
b"\x00" as *const u8 as *const libc::c_char
},
if !final_text.is_null()
final_text.as_ptr(),
if final_text != CString::yolo("")
&& !footer.is_null()
&& 0 != *footer.offset(0isize) as libc::c_int
{
Expand Down Expand Up @@ -1094,8 +1096,17 @@ unsafe fn get_subject(
) -> *mut libc::c_char {
let context = (*chat).context;
let ret: *mut libc::c_char;
let raw_subject =
dc_msg_get_summarytext_by_raw((*msg).type_0, (*msg).text, &mut (*msg).param, 32, context);

let raw_subject = {
let msgtext_c = (*msg)
.text
.as_ref()
.map(|s| CString::yolo(String::as_str(s)));
let msgtext_ptr = msgtext_c.map_or(ptr::null(), |s| s.as_ptr());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is nice! I think there are a lot of places that simply do s.as_ptr() instead of the full .map_or(ptr::null(), |s| s.as_ptr()) and that's probably the cause of a bunch of crashes.


dc_msg_get_summarytext_by_raw((*msg).type_0, msgtext_ptr, &mut (*msg).param, 32, context)
};

let fwd = if 0 != afwd_email {
b"Fwd: \x00" as *const u8 as *const libc::c_char
} else {
Expand Down
Loading