Fix UB in number tokenizer: use pointer arithmetic instead of derefer…#5
Merged
Conversation
…encing end iterator ## Summary The number-literal scanner in `Tokenizer.cpp` passed `&*byteStart` and `&*byteEnd` to `std::regex_search`, where `byteEnd` is `path_.cend()`. Dereferencing a past-the-end iterator (`*path_.cend()`) is **undefined behavior**, even when only its address is taken. Most standard libraries (libstdc++, libc++) happen to tolerate this, but MSVC's checked iterators (`_ITERATOR_DEBUG_LEVEL`) correctly reject it, causing an assertion failure / test crash on Windows MSVC 2022 builds. ## Fix Form the regex range from `path_.data()` pointers instead of dereferencing iterators: ```cpp const char* byteStart = path_.data() + byte_offsets_[position_]; const char* byteEnd = path_.data() + path_.size(); std::cmatch match; if (std::regex_search(byteStart, byteEnd, match, numregex) && ...) ``` `data() + size()` is a valid one-past-the-end **pointer** (legal to form, never dereferenced), which is exactly what `std::regex_search` expects for its `[first, last)` range. `std::cmatch` already operates on `const char*` ranges, so the surrounding match handling is unchanged. No behavioral change on conforming platforms — this only removes the UB that MSVC flags. ## Testing - All existing generated tests pass. Reported by an external contributor building with MSVC 2022 on Windows. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
…encing end iterator
Summary
The number-literal scanner in
Tokenizer.cpppassed&*byteStartand&*byteEndtostd::regex_search, wherebyteEndispath_.cend(). Dereferencing a past-the-end iterator (*path_.cend()) is undefined behavior, even when only its address is taken.Most standard libraries (libstdc++, libc++) happen to tolerate this, but MSVC's checked iterators (
_ITERATOR_DEBUG_LEVEL) correctly reject it, causing an assertion failure / test crash on Windows MSVC 2022 builds.Fix
Form the regex range from
path_.data()pointers instead of dereferencing iterators:data() + size()is a valid one-past-the-end pointer (legal to form, never dereferenced), which is exactly whatstd::regex_searchexpects for its[first, last)range.std::cmatchalready operates onconst char*ranges, so the surrounding match handling is unchanged. No behavioral change on conforming platforms — this only removes the UB that MSVC flags.Testing
Reported by @u19809 while building with MSVC 2022 on Windows.