Repository navigation
Conversation
svector::push_back, insert(pos, value) and assign(n, value) read their argument after calling grow(). grow() frees the old heap buffer, so when the argument refers to an element of the same svector and the call has to reallocate, the read is a heap-use-after-free. On a full heap-backed svector, v.push_back(v[0]), v.insert(v.begin(), v[0]) and v.assign(5, v[0]) store garbage, and AddressSanitizer reports the read. insert() already adjusted the argument pointer when it fell inside the shifted range. That covered the shift but not the reallocation. When the argument is a separate object, GCC 13 and 15 also follow the branch where it lies inside the vector and warn that the adjusted pointer reads past it (-Warray-bounds). The three functions now copy the value before anything can move or free it. push_back only makes the copy when it reallocates. insert() treats pos == end() like any other position, and the pointer adjustment is gone, along with its -Warray-bounds warnings.
Contributor
Merging this PR will regress 1 benchmark1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
This branch has not been deployed
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.
Checklist
Description
svector::push_back,insert(pos, value)andassign(n, value)read their argument aftergrow(). When the call reallocates,grow()frees the old heap buffer, so an argument that refers to an element of the samesvectordangles:AddressSanitizer reports a heap-use-after-free in each case. Without it, the inserted values are garbage.
insert()already adjusted the argument pointer when it fell inside the shifted range, but that did not cover the reallocation. That adjustment is also the source of the-Warray-boundswarning GCC 13 and 15 emit at that line: when the argument is a separate object, GCC follows the branch where it lies inside the vector and sees a read past it.The three functions now copy the value before anything can move or free it.
push_backonly copies when it reallocates.insert()treatspos == end()like any other position, and the pointer adjustment is gone, along with its warnings.svector.insert_aliased_element,svector.push_back_aliased_elementandsvector.assign_aliased_elementcover these cases. On master all three fail under AddressSanitizer, and 6 of their checks also fail in a plain-O2build. They pass with the fix.