Merge pull request #234 from KAction/send-garbage

Send garbage
This commit is contained in:
Friedel Ziegelmayer
2019-07-27 12:30:35 +02:00
committed by GitHub
4 changed files with 25 additions and 4 deletions

View File

@@ -450,6 +450,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
dellocations\n\
getlocations [<contact-id>]\n\
send <text>\n\
send-garbage\n\
sendimage <file> [<text>]\n\
sendfile <file>\n\
draft [<text>]\n\
@@ -948,6 +949,15 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
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(

View File

@@ -3,6 +3,7 @@
//!
//! Usage: cargo run --example repl --release -- <databasefile>
//! All further options can be set using the set-command (type ? for help).
#![feature(ptr_cast)]
#[macro_use]
extern crate deltachat;
@@ -290,7 +291,7 @@ const DB_COMMANDS: [&'static str; 11] = [
"housekeeping",
];
const CHAT_COMMANDS: [&'static str; 24] = [
const CHAT_COMMANDS: [&'static str; 25] = [
"listchats",
"listarchived",
"chat",
@@ -308,6 +309,7 @@ const CHAT_COMMANDS: [&'static str; 24] = [
"dellocations",
"getlocations",
"send",
"send-garbage",
"sendimage",
"sendfile",
"draft",

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);

View File

@@ -4,6 +4,7 @@ use std::fs;
use std::time::SystemTime;
use chrono::{Local, TimeZone};
use failure::format_err;
use mmime::mailimf_types::*;
use rand::{thread_rng, Rng};
@@ -1563,13 +1564,16 @@ pub fn to_string_lossy(s: *const libc::c_char) -> String {
}
pub fn as_str<'a>(s: *const libc::c_char) -> &'a str {
as_str_safe(s).unwrap_or_else(|err| panic!("{}", err))
}
pub fn as_str_safe<'a>(s: *const libc::c_char) -> Result<&'a str, failure::Error> {
assert!(!s.is_null(), "cannot be used on null pointers");
let cstr = unsafe { CStr::from_ptr(s) };
cstr.to_str().unwrap_or_else(|err| {
panic!("Non utf8 string: '{:?}' ({:?})", cstr.to_bytes(), err);
})
cstr.to_str()
.map_err(|err| format_err!("Non utf8 string: '{:?}' ({:?})", cstr.to_bytes(), err))
}
/// Convert a C `*char` pointer to a [std::path::Path] slice.