Fix palette-packing decant logic to avoid UB from invalid iterator#2031
Merged
Conversation
The "Iterate through the component's color sets, and transfer them" logic shifted `from.begin()`, which would make subsequent `std::advance` calls go past `_assigned.end()` and cause UB. This rewrite uses absolute numeric indexes into `_assigned` to avoid that potential problem. I haven't been able to craft a test case that actually *reaches* that piece of logic, and I suspect that it's unreachable given how we sort and process color sets before decanting, but cannot prove that; so it stays in. (Maybe if we added 4bpp 16-colors-per-palette support, it would become reachable?)
ISSOtm
requested changes
Jul 23, 2026
ISSOtm
left a comment
Member
There was a problem hiding this comment.
Logic looks sound, I only have some comments regarding the code style itself
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.
The "Iterate through the component's color sets, and transfer them" logic shifted
from.begin(), which would make subsequentstd::advancecalls go past_assigned.end()and cause UB. This rewrite uses absolute numeric indexes into_assignedto avoid that potential problem.I haven't been able to craft a test case that actually reaches that piece of logic, and I suspect that it's unreachable given how we sort and process color sets before decanting, but cannot prove that; so it stays in. (Maybe if we added 4bpp 16-colors-per-palette support, it would become reachable?)
This approach can be compared with the
pal_packing.rsport, which also uses absolute indexes (but changed how color sets work so there's no extra complexity of skipping over free slots).