Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xkbcomp: Fix incorrect memory comparison #661

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

wismill
Copy link
Member

@wismill wismill commented Feb 13, 2025

Spotted by clang-tidy:

../src/keymap-priv.c:146:16: warning: comparing object representation of
                             type 'union xkb_action' which does not have
                             a unique object representation; consider
                             comparing the members of the object manually
                             [bugprone-suspicious-memory-comparison]

Indeed, from “EXP42-C. Do not compare padding data”1:

unnamed members of objects of structure and union type do not participate
in initialization. Unnamed members of structure objects have indeterminate
representation even after initialization.

But since we zero the action, I do not think the current behavior can cause a bug, can it?

Fixed by using a dedicated comparison function for union xkb_action.

Footnotes

  1. https://wiki.sei.cmu.edu/confluence/display/c/EXP42-C.+Do+not+compare+padding+data

Spotted by clang-tidy:

```
../src/keymap-priv.c:146:16: warning: comparing object representation of
                             type 'union xkb_action' which does not have
                             a unique object representation; consider
                             comparing the members of the object manually
                             [bugprone-suspicious-memory-comparison]
````

Indeed, from “EXP42-C. Do not compare padding data”[^1]:

> unnamed members of objects of structure and union type do not participate
> in initialization. Unnamed members of structure objects have indeterminate
> representation even after initialization.

Fixed by using a dedicated comparison function for `union xkb_action`.

[^1]: https://wiki.sei.cmu.edu/confluence/display/c/EXP42-C.+Do+not+compare+padding+data
@wismill wismill added the compiler warning Compiler emits warnings that should be avoided label Feb 13, 2025
@wismill wismill added this to the 1.9.0 milestone Feb 13, 2025
@wismill wismill requested review from bluetech and whot February 13, 2025 19:40
@wismill wismill mentioned this pull request Feb 13, 2025
Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (a->type != b->type)
return false;

/* Ensure we support all action types */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh that's clever :)

Copy link
Member Author

@wismill wismill Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a minimal safeguard against changes in the actions types, but I do not know how to check if we were to add new fields. Well, this kind of refactor is unlikely, but who knows?

@wismill wismill merged commit 5ef19bc into xkbcommon:master Feb 14, 2025
5 checks passed
@wismill wismill deleted the actions/fix-memcmp branch February 14, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler warning Compiler emits warnings that should be avoided
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants