Skip to content

xkbinfo: only insert new layouts, skip over duplicate ones

This matches the behavior to the one in the old code path before libxkbregistry.

This also fixes a use-after-free bug when a duplicate layout is present. The same layout struct is a member of multiple hashtables, specifically priv->layouts_table, priv->layouts_by_language and priv->layouts_by_country.

When the duplicate layout is added, add_layouts calls g_hash_table_replace (priv->layouts_table, l->id, l) which frees the original layout - but the layouts_by_{country|language} still have that now-freed layout. Immediately afterwards, add_layouts calls add_layout_to_locale_tables () which calls add_layout_to_table () which triggers a use-after-free.

Avoid all this by simply skipping any duplicate layout.

Reproducible with gsettings set org.gnome.desktop.input-sources show-all-sources true valgrind /usr/libexec/gnome-desktop-debug/test-xkb-info

Requires xkeyboard-config <= 2.32, it has a duplicate cm(mmuock) entry (one is marked exotic, hence the need for show-all-sources).

Fixes #190 (closed)

Note: I opted for this approach instead of !110 (closed) because it makes the code clearer and matches the previous behaviour. !110 (closed) IMO relies on too much knowledge of which element is referenced where, easier to just skip over duplicated entries. If we did want to fix this properly, we should make the entries refcounted instead.

cc @pobrn, @jf

Merge request reports