Fix undefined behavior in __pointer_get_max_taggable#11
Open
hobostay wants to merge 1 commit into
Open
Conversation
The expression `(1 << total)` uses an `int` literal, which causes undefined behavior when `total >= 31` (i.e. when the combined tag bits equal or exceed the width of `int`). Use `1ULL` instead to ensure the shift is well-defined for any valid tag configuration. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
SIGFUN
suggested changes
May 4, 2026
| { | ||
| size_t total = POINTER_TAG_BITS_HI + POINTER_TAG_BITS_LO; | ||
| return (ptrtag_t)((1 << total) - 1); | ||
| return (ptrtag_t)((1ULL << total) - 1); |
Contributor
There was a problem hiding this comment.
ptrtag_t is a uint32_t, should we instead be doing (ptrtag_t)1?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
__pointer_get_max_taggable()insrc/0xc/std/pointer.cDetails
The expression
(1 << total)on line 62 uses anintliteral1. In C, shifting anintby>= 31bits is undefined behavior (per C11 §6.5.7p4: the behavior is undefined if the right operand is greater than or equal to the width of the promoted left operand).While the current default configurations use
POINTER_TAG_BITS_HI=16andPOINTER_TAG_BITS_LO=3(total=19, which avoids UB), this function is part of a public API and could be used with configurations that have more tag bits. For example, a 32-bit target could legitimately use tag bits summing to 31 or more.The fix changes
(1 << total)to(1ULL << total), ensuring the shift is well-defined for any validtotalvalue (up to 64).Test plan
make test)🤖 Generated with Claude Code