-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: make the wrapped successor explicit in CRangesSet under -fsanitize=integer #7590
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,16 @@ | |
|
|
||
| #include <limits> | ||
|
|
||
| namespace { | ||
| //! Successor of a value in the half-open range encoding: the range containing | ||
| //! UINT64_MAX stores a wrapped end of 0. Spelled as a branch so the wrap is | ||
| //! explicit intent rather than arithmetic overflow (-fsanitize=integer). | ||
| constexpr uint64_t WrappedSuccessor(uint64_t value) noexcept | ||
| { | ||
| return value == std::numeric_limits<uint64_t>::max() ? 0 : value + 1; | ||
| } | ||
| } // namespace | ||
|
|
||
| CRangesSet::Range::Range() : CRangesSet::Range::Range(0, 0) {} | ||
|
|
||
| CRangesSet::Range::Range(uint64_t begin_in, uint64_t end_in) : | ||
|
|
@@ -24,7 +34,7 @@ bool CRangesSet::Add(uint64_t value) | |
| // all 3 of them should be merged in one range [x, y) | ||
| // - if there's exist a range [x, value) - we need to replace it to new range [x, value + 1) | ||
| // - if there's exist a range [value + 1, y) - we need to replace it to new range [value, y) | ||
| Range new_range{value, value + 1}; | ||
| Range new_range{value, WrappedSuccessor(value)}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Update the adjacent At Line 37, 🤖 Prompt for AI Agents |
||
| auto it = ranges.lower_bound({value, value}); | ||
| if (it != ranges.begin()) { | ||
| auto prev = it; | ||
|
|
@@ -37,7 +47,7 @@ bool CRangesSet::Add(uint64_t value) | |
| } | ||
| const auto next = it; | ||
| if (next != ranges.end()) { | ||
| if (next->begin == value + 1) { | ||
| if (next->begin == WrappedSuccessor(value)) { | ||
| new_range.end = next->end; | ||
| ranges.erase(next); | ||
| } | ||
|
|
@@ -67,8 +77,8 @@ bool CRangesSet::Remove(uint64_t value) | |
| const auto ret = ranges.insert({current_range.begin, value}); | ||
| assert(ret.second); | ||
| } | ||
| if (value + 1 != current_range.end) { | ||
| const auto ret = ranges.insert({value + 1, current_range.end}); | ||
| if (WrappedSuccessor(value) != current_range.end) { | ||
| const auto ret = ranges.insert({WrappedSuccessor(value), current_range.end}); | ||
| assert(ret.second); | ||
| } | ||
| return true; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Nitpick: Describe the wrapped successor in Add() comments
The implementation now deliberately uses
WrappedSuccessor(value), but the adjacent algorithm comments still describe every endpoint asvalue + 1. ForUINT64_MAX, the stored endpoint is actually0, so these comments obscure the boundary behavior this PR makes explicit. Use the helper's name in the range descriptions and document its wrapped endpoint.source: ['coderabbit']