Skip to content

Commit

Permalink
Fix bug when clipboard has non-string data, close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ippee committed Jul 29, 2021
1 parent 7eba556 commit 15316e3
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ use std::os::raw::c_char;
pub extern "C" fn get_contents() -> *const c_char {
let mut ctx: ClipboardContext = ClipboardProvider::new().expect("ClipboardProvider::new failed");

match ctx.get_contents() {
Ok(v) => {
let cstring = CString::new(v).expect("CString::new failed");
return cstring.into_raw();
},
Err(_e) => panic!("Failed to get the clipboard contents"),
}
let cstring = match ctx.get_contents() {
Ok(v) => CString::new(v).expect("CString::new failed"),
Err(_e) => CString::new("").expect("CString::new failed"),
};

return cstring.into_raw();
}

#[no_mangle]
Expand All @@ -40,7 +39,7 @@ mod tests {
use std::ffi::CString;

#[test]
fn set_get_clipboard_contents() {
fn set_get_clipboard_contents() {
let contents = CString::new("All the world's a stage").expect("CString::new failed");
let expected_ptr = contents.into_raw();

Expand Down

0 comments on commit 15316e3

Please sign in to comment.