Check if input to dc_send_text_msg is valid utf8

With this change, passing invalid utf8 string to `dc_send_text_msg' does not
crash application, it prints warning and returns error code.

It should be admitted that this fix is sub-optimal: if input C string is valid
utf8 (which is likely), result of successful conversion to `&str' is discarded
in `dc_send_text_msg', and the same input C string is converted again with
`as_str' in `prepare_msg_raw'.

It is not clear how to fix it in non-disruptive way, since input C string is
passed down to call stack as part of `dc_msg_t' struct, which is part of C ABI.
This commit is contained in:
Dmitry Bogatov
2019-07-27 09:04:20 +00:00
parent d1968d8ccb
commit 36b5f4da53
3 changed files with 12 additions and 4 deletions

View File

@@ -976,6 +976,11 @@ pub unsafe fn dc_send_text_msg(
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, 10);
(*msg).text = dc_strdup(text_to_send);
let ret = dc_send_msg(context, chat_id, msg);