Fixed memory bug: properly root repeated/map field when assigning. #8639
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.
Previously the protobuf extension would not properly root memory from a repeated field or map when assigning to a message field (see the attached test case). If there is no direct reference to the repeated field from Ruby, this will result in a memory error if there is a GC and the repeated field is subsequently accessed (for example, in
#encode
).The fix is to do the appropriate
upb_arena_fuse()
operation when this assignment is performed. This will cause the Union/Find machinery to ensure that the memory for the repeated field or map lives as long as the message.The the new test case does not actually crash on my machine, but it does trigger a Valgrind error that I can confirm is fixed by the code in this PR. In the future we should make sure that our CI tests are running tests under Valgrind so we can catch future memory errors of this kind.
Also added an explicit check that
upb_arena_fuse()
succeeded. This is not related to the bugfix, but it is another measure to help ensure that the memory model of the extension is correct.Fixes: #8559