quininer / x11-clipboard

x11 clipboard support for Rust
MIT License
43 stars 29 forks source link

load_wait crash when copying from chrome address bar #31

Open wrvsrx opened 1 year ago

wrvsrx commented 1 year ago

Reproduce

  1. compile and run following script with RUST_BACKTRACE=1:

    use x11_clipboard::Clipboard;
    fn main() {
      let clipboard = Clipboard::new().unwrap();
    
      loop {
          let val = clipboard
              .load_wait(
                  clipboard.setter.atoms.clipboard,
                  clipboard.setter.atoms.utf8_string,
                  clipboard.setter.atoms.property
              )
              .unwrap();
          println!("{}", val.len());
    
          let val = String::from_utf8(val).unwrap();
    
          println!("{}", val);
      }
    }
  2. copy from chrome address bar
  3. It crashes with following information:
    23
    https://www.google.com/
    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnexpectedType(0)', src/main.rs:12:14
    stack backtrace:
    0: rust_begin_unwind
    1: core::panicking::panic_fmt
    2: core::result::unwrap_failed
    3: core::result::Result<T,E>::unwrap
             at /build/rustc-1.65.0-src/library/core/src/result.rs:1107:23
    4: x11_test::main
             at ./src/main.rs:6:19
    5: core::ops::function::FnOnce::call_once
             at /build/rustc-1.65.0-src/library/core/src/ops/function.rs:248:5
    note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Reason

Google chrome has a strange behavior: it send xcb_xfixes_selection_notify_event_t twice when coping from address bar. Therefore loop in process_event meet XfixesSelectionNotify (https://github.com/quininer/x11-clipboard/blob/6d850c069feb6aab0b3869e7ca1f76298f84546e/src/lib.rs#L182) twice at first and call convert_selection twice (https://github.com/quininer/x11-clipboard/blob/6d850c069feb6aab0b3869e7ca1f76298f84546e/src/lib.rs#L183) before recive SelectioniNotify (you can verify it by log), which makes reply.type_ become zero and UnexpectedType error raise.

Fix

After get XfixesSelectionNotify, only wait for SelectionNotify and PropertyNotify.

Maybe I can make a PR later.

quininer commented 1 year ago

This is not a crash, load_wait returns an error as expected.

The crate is in a maintenance state and I am not willing to make behavior change.