module/sway-xkb: don’t add the “same” device multiple times

Not sure if Sway bug or not, but we’ve seen Sway presenting multiple
input devices with the exact same ID (and nothing else differentiating
them).

This caused a crash in the sway-xkb module, since we didn’t check if
we were already tracking the device, and thus bumped the
“num_existing_inputs” variable multiple times for the same input
object.

This lead to a content() returning an array with uninitialized
elements, and thus a crash.

Closes #229
This commit is contained in:
Daniel Eklöf 2022-10-04 21:09:43 +02:00
parent 6ed576c719
commit 028011a816
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 12 additions and 1 deletions

View file

@ -115,6 +115,8 @@
* network: missing SSID (recent kernels, or possibly wireless drivers, * network: missing SSID (recent kernels, or possibly wireless drivers,
no longer provide the SSID in the `NL80211_CMD_NEW_STATION` no longer provide the SSID in the `NL80211_CMD_NEW_STATION`
response) ([#226][226]). response) ([#226][226]).
* sway-xkb: crash when compositor presents multiple inputs with
identical IDs ([#229][229]).
[169]: https://codeberg.org/dnkl/yambar/issues/169 [169]: https://codeberg.org/dnkl/yambar/issues/169
[172]: https://codeberg.org/dnkl/yambar/issues/172 [172]: https://codeberg.org/dnkl/yambar/issues/172
@ -123,6 +125,7 @@
[198]: https://codeberg.org/dnkl/yambar/issues/198 [198]: https://codeberg.org/dnkl/yambar/issues/198
[221]: https://codeberg.org/dnkl/yambar/issues/221 [221]: https://codeberg.org/dnkl/yambar/issues/221
[226]: https://codeberg.org/dnkl/yambar/issues/226 [226]: https://codeberg.org/dnkl/yambar/issues/226
[229]: https://codeberg.org/dnkl/yambar/issues/229
### Security ### Security

View file

@ -67,6 +67,7 @@ content(struct module *mod)
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
assert(m->num_existing_inputs <= m->num_inputs);
struct exposable *particles[max(m->num_existing_inputs, 1)]; struct exposable *particles[max(m->num_existing_inputs, 1)];
for (size_t i = 0, j = 0; i < m->num_inputs; i++) { for (size_t i = 0, j = 0; i < m->num_inputs; i++) {
@ -120,9 +121,12 @@ handle_input_reply(int type, const struct json_object *json, void *_mod)
struct input *input = NULL; struct input *input = NULL;
for (size_t i = 0; i < m->num_inputs; i++) { for (size_t i = 0; i < m->num_inputs; i++) {
struct input *maybe_input = &m->inputs[i]; struct input *maybe_input = &m->inputs[i];
if (strcmp(maybe_input->identifier, id) == 0) { if (strcmp(maybe_input->identifier, id) == 0 && !maybe_input->exists)
{
input = maybe_input; input = maybe_input;
LOG_DBG("adding: %s", id);
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
input->exists = true; input->exists = true;
m->num_existing_inputs++; m->num_existing_inputs++;
@ -209,6 +213,8 @@ handle_input_event(int type, const struct json_object *json, void *_mod)
if (is_removed) { if (is_removed) {
if (input->exists) { if (input->exists) {
LOG_DBG("removing: %s", id);
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
input->exists = false; input->exists = false;
m->num_existing_inputs--; m->num_existing_inputs--;
@ -220,6 +226,8 @@ handle_input_event(int type, const struct json_object *json, void *_mod)
if (is_added) { if (is_added) {
if (!input->exists) { if (!input->exists) {
LOG_DBG("adding: %s", id);
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
input->exists = true; input->exists = true;
m->num_existing_inputs++; m->num_existing_inputs++;