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

Fix numbers types #605

Merged
merged 12 commits into from
Feb 12, 2025
Merged

Fix numbers types #605

merged 12 commits into from
Feb 12, 2025

Conversation

wismill
Copy link
Member

@wismill wismill commented Jan 21, 2025

WIP, made public only to make you aware I am working on this.

Follow-up of #604 that is mostly a refactor of the parser for integers. Will probably split the minor refactoring in another PR.

Fixes #594

@wismill wismill added compile-keymap Indicates a need for improvements or additions to keymap compilation compiler warning Compiler emits warnings that should be avoided labels Jan 21, 2025
@wismill wismill added this to the 1.9.0 milestone Jan 21, 2025
@wismill wismill force-pushed the xkbcomp/fix-numbers branch from 24b0e78 to 01b895e Compare February 5, 2025 15:36
@wismill wismill force-pushed the xkbcomp/fix-numbers branch 2 times, most recently from 6146341 to 128b12e Compare February 12, 2025 08:25
@wismill wismill requested review from bluetech and whot and removed request for bluetech February 12, 2025 08:28
@wismill wismill marked this pull request as ready for review February 12, 2025 08:28
@wismill wismill force-pushed the xkbcomp/fix-numbers branch 2 times, most recently from d7b7ad8 to ce3e9d2 Compare February 12, 2025 09:15
@bluetech
Copy link
Member

Some possible followups:

  • In xkbcomp/scanner we should change the strtoul to strtol since we're using int64_t. While integer literal cannot be negative, the cast of unsigned long to int64_t is lossy. (We could change ival to unsigned but then it just defers the cast so I think we shouldn't).
  • We should probably check the add/sub/mul in expr.c for overflows/underflows and error if it happens. C23 has stdckdint.h, maybe we can use some backport of it.

I will try to do this unless you feel like it :)

@wismill wismill force-pushed the xkbcomp/fix-numbers branch from ce3e9d2 to cf93441 Compare February 12, 2025 09:28
@wismill
Copy link
Member Author

wismill commented Feb 12, 2025

  • In xkbcomp/scanner we should change the strtoul to strtol since we're using int64_t. While integer literal cannot be negative, the cast of unsigned long to int64_t is lossy. (We could change ival to unsigned but then it just defers the cast so I think we shouldn't).

@bluetech I agree, it showed multiple times already with clang-tidy. But it’s not ideal, as strtol returns a long but we want an int64_t.

  • We should probably check the add/sub/mul in expr.c for overflows/underflows and error if it happens. C23 has stdckdint.h, maybe we can use some backport of it.

While I agree that would make our implementation more robust:

  1. I think I have never seen those in real use.
  2. We already use 64 bits ints, although all our ints are max 32 bits. So we already have some great margin.

Do you have a realistic use case in mind?

@bluetech
Copy link
Member

@bluetech I agree, it showed multiple times already with clang-tidy. But it’s not ideal, as strtol returns a long but we want an int64_t.

We can use strtoll then I suppose.

I think I have never seen those in real use.

It's new in C23 so probably no one uses it yet, instead using gcc builtins or custom code.

We already use 64 bits ints, although all our ints are max 32 bits. So we already have some great margin.

Do you mean before or after this PR? I think that before, we are using int for both operands and result, so overflow is possible, and after, we are using int64_t for both operands and result, so overflow is still possible. Do I have it wrong?

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.

Looks great! This was very much needed.

@wismill
Copy link
Member Author

wismill commented Feb 12, 2025

I think I have never seen those in real use.

It's new in C23 so probably no one uses it yet, instead using gcc builtins or custom code.

@bluetech I meant the int operations in XKB files, not the C23 functions.

Do you mean before or after this PR? I think that before, we are using int for both operands and result, so overflow is possible, and after, we are using int64_t for both operands and result, so overflow is still possible. Do I have it wrong?

You are right, but what I mean is that we do not expect the user to input numbers > 32 bits. So after we will handle 32 bits correctly, with some margin to handle overflow and report it. But if the int64_t overflows, then indeed we would not catch it. But I find it very unlikely, see my comment about the use of operations before.

@bluetech
Copy link
Member

Oh yes, there is definitely no real use case for doing such things. And the arithmetic isn't even allowed in many places. But I just feel bad about allowing user input to trigger UB (signed int overflow). We could use -fwrapv but it's not portable.

@wismill wismill force-pushed the xkbcomp/fix-numbers branch from cf93441 to e4c0c72 Compare February 12, 2025 14:55
@wismill wismill requested a review from bluetech February 12, 2025 14:55
Avoid implicit conversion from `int64_t`.
The `Resolve*` functions do not always initialize the parameters
that they can modify, so it is safer to always initialize them at the
call site.
@wismill wismill force-pushed the xkbcomp/fix-numbers branch from e4c0c72 to b0ac4bd Compare February 12, 2025 14:58
@wismill
Copy link
Member Author

wismill commented Feb 12, 2025

@bluetech I tackled the strtoul issue and fixed 2 more silent casts for compat group and LED index. Added some tests too.

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.

The extra changes LGTM

@wismill wismill force-pushed the xkbcomp/fix-numbers branch from b0ac4bd to 592ca3e Compare February 12, 2025 17:27
@wismill
Copy link
Member Author

wismill commented Feb 12, 2025

Latch batch for this PR. There are still cast warnings, but I leave it for a further PR.

@wismill
Copy link
Member Author

wismill commented Feb 12, 2025

Note to self: check the Windows CI logs for further cast warnings!

@wismill wismill merged commit 350931a into xkbcommon:master Feb 12, 2025
5 checks passed
@wismill wismill deleted the xkbcomp/fix-numbers branch February 12, 2025 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compile-keymap Indicates a need for improvements or additions to keymap compilation compiler warning Compiler emits warnings that should be avoided
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Virtual modifier explicit mapping is limited to platform-dependent int size
2 participants