arminfriedl / xcb-wm

Rust implementation of xcb-wm - icccm and ewmh extensions for xcb
MIT License
5 stars 3 forks source link

Indexing into invalid replies causes panic #5

Open fiorematteo opened 1 year ago

fiorematteo commented 1 year ago

I'm having trouble with getting the _NET_ACTIVE_WINDOW atom right after X is started. Most likely it's due to the fact that at that time the atom is not set. Xcb doesn't returns an error and instead returns an invalid reply. After that the bogus reply is passed to this from function.

impl From<xcb::x::GetPropertyReply> for GetActiveWindowReply {
    fn from(reply: xcb::x::GetPropertyReply) -> Self {
        GetActiveWindowReply {
            window: unsafe { xcb::x::Window::new(reply.value::<u32>()[0]) },
        }
    }
}

reply.value::<u32>() returns an empty slice so indexing it panics.

This is what a bad reply looks like:

&reply.bytes_after() = 0
&reply.length() = 0
&reply.r#type() = Atom { res_id: 0, }
&reply.response_type() = 1
&reply.sequence() = 167
&reply.value::<u32>() = []

Trying to debug print reply blows up with this scary error

&reply = thread 'main' panicked at 'internal error: entered unreachable code: impossible prop format: 0', /home/matteo/progetti/rust/barust/target/release/build/xcb-cc126fbef2fc4c4d/out/xproto.rs:13665:23

It looks like this should be a problem with other atoms but I didn't try. I think this should be fixed in the caller (wait_for_reply and wait_for_reply_unchecked) by checking if the returned reply has 0 length. Something like this:

    pub fn wait_for_reply<C>(&self, cookie: C) -> xcb::Result<C::Reply>
    where
        C: EwmhPropertyCookieChecked,
    {
        let xcb_reply = self.con.wait_for_reply(cookie.inner());
        match xcb_reply{
            Ok(reply) => {
                if reply.length() == 0{
                    return Err(/*not sure which error to use here*/)
                }
                else{
                    Ok(reply.into())
                }
            },
            Err(e) => Err(e),
        }
    }