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/ast-build: fix possible UB in expr AST node allocations #659

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bluetech
Copy link
Member

@bluetech bluetech commented Feb 8, 2025

The expression AST constructors all return ExprDef *. ExprDef is a union of all expr types. As a memory optimization, instead of allocating sizeof(ExprDef), we only allocate the size of the actual type (e.g. sizeof(ExprBinary)) which is sometimes smaller than sizeof(ExprDef). This is probably undefined behavior, and gcc (with optimization turned on) complains about it, for example:

src/xkbcomp/ast-build.c:69:23: warning: array subscript ‘ExprDef[0]’ is partly outside array bounds of ‘unsigned char[24]’ [-Warray-bounds=]

Since it doesn't save that much memory, drop this optimization.

Fix #292.

The expression AST constructors all return `ExprDef *`. `ExprDef` is a
union of all expr types. As a memory optimization, instead of allocating
`sizeof(ExprDef)`, we only allocate the size of the actual type (e.g.
`sizeof(ExprBinary)`) which is sometimes smaller than `sizeof(ExprDef)`.
This is probably undefined behavior, and gcc (with optimization turned
on) complains about it, for example:

src/xkbcomp/ast-build.c:69:23: warning: array subscript ‘ExprDef[0]’ is partly outside array bounds of ‘unsigned char[24]’ [-Warray-bounds=]

Since it doesn't save that much memory, drop this optimization.

Fix xkbcommon#292.

Signed-off-by: Ran Benita <[email protected]>
@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 Feb 11, 2025
@wismill wismill added this to the 1.9.0 milestone Feb 11, 2025
Copy link
Member

@wismill wismill left a comment

Choose a reason for hiding this comment

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

A thought: we could still revive this with your bump allocator.

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.

compiling with gcc-12 generates lots of -Warray-bounds warnings in ast-build.c
2 participants