Skip to content

Fix UB in const_fixed_iterator with negative offsets - #164

Merged
joto merged 1 commit into
masterfrom
kk/fix-fixed-iterator-negative-offset
Oct 15, 2026
Merged

joto merged 1 commit into
masterfrom
kk/fix-fixed-iterator-negative-offset

Conversation

@kkaefer

@kkaefer kkaefer commented Oct 14, 2026

Copy link
Copy Markdown
Member

Moving a fixed-size packed iterator backwards by a negative offset calculated the byte offset with unsigned arithmetic. The negative value wrapped around to a huge unsigned number before it was added to the pointer. On all of the platforms we support, we likely still computed the correct address by accident, but strictly speaking, though, it's pointer overflow, which is UB and UBSan flags it. This only affects backwards moves with a negative offset, indexing with a negative index and std::prev().

The offset is now computed in signed arithmetic. I added a unit test for negative offsets, which fails under UBSan without this change. We should probably add a few sanitizer jobs to our CI as well to catch issues like this.

sizeof(value_type) * val converted a negative difference_type to unsigned,
so it += -1, it[-1] and std::prev() overflowed the pointer. Cast the size
to difference_type before multiplying.
@kkaefer
kkaefer requested a review from a team as a code owner October 14, 2026 09:34
@kkaefer
kkaefer requested a review from joto October 14, 2026 09:35
@kkaefer

kkaefer commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

Sanitizer PR is in #165

@joto
joto merged commit 340f5aa into master Oct 15, 2026
67 checks passed
@kkaefer
kkaefer deleted the kk/fix-fixed-iterator-negative-offset branch October 15, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants