rust-x-bindings / rust-xcb

Rust bindings and wrapper for XCB.
MIT License
165 stars 64 forks source link

`GetPropertyReply::value` causes a crash when the length and format are zero #154

Closed RealKC closed 2 years ago

RealKC commented 2 years ago

Here is a reproducer: https://gist.github.com/RealKC/3f472c7baa19ba27c88af27e0abcc494. If you uncomment the innermost if, the program will happily print the atoms for the windows which have _NET_WM_STATE set on them, if you keep it commented, the program will eventually maybe crash with the following assertion only if the WM returns properties with length 0 and format 0 on them:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `32`: mismatched format of xproto::GetPropertyReply::value', /home/kc/programming/xcbbtest/target/debug/build/xcb-bb99a98c60179771/out/xproto.rs:13675:9

Note that this isn't reproducible everywhere, this happens for me on awesomewm but not to a friend who uses KDE.

Some possible fixes for the issue:

I'm not sure which of those is more elegant, but as an end-user I'd like to handle this case in some manner and I'm not sure checking the length would always work (I assume yes, since if the WM is sending 0 for format, you can't really use the reply anyway) and I don't exactly believe this is going against the EWMH spec? Some properties have type/format next to their name(at least that's how I interpret it, maybe I missed something in the spec), like _NET_WM_WINDOW_TYPE(https://specifications.freedesktop.org/wm-spec/latest/ar01s05.html#idm46476783509232) which has ATOM[]/32 but _NET_WM_STATE only has ATOM[] see https://specifications.freedesktop.org/wm-spec/latest/ar01s05.html#idm46476783496896

rtbo commented 2 years ago

As far as I know, format is always supposed to be either 8, 16 or 32. But I don't think I encountered zero length props already. What is the value of format when length is not zero for the same property? Is it 32?

I can easily fix this by checking the length before the assertion.

RealKC commented 2 years ago

What is the value of format when length is not zero for the same property? Is it 32?

Yeah it's always either 32(when length is nonzero) or 0(when length is zero) from my testing

rtbo commented 2 years ago

It should be OK now. Don't hesitate to ask to reopen if not.

RealKC commented 2 years ago

I just made the test program use xcb from git and it works! Thanks a lot :)