Repository navigation
Copy tag directives in Emitter#start_document - #831
Open
ninjapanzer wants to merge 1 commit into
Open
ninjapanzer wants to merge 1 commit into
ninjapanzer wants to merge 1 commit into
Conversation
start_document stored raw pointers into the handle and prefix Strings of each tag directive, but libyaml copies those bytes only after the whole loop has run. Nothing kept an exported String alive once the next iteration reassigned the local, and a String still referenced from the tags array could be moved by compaction, so libyaml could read freed or relocated memory. Copy the bytes with ruby_strdup and free the copies in start_document_ensure. Fix ruby#812
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.
Fix #812
Psych::Emitter#start_documentstored raw pointers into the handle and prefix Strings of each tag directive, but libyaml copies those bytes only inyaml_document_start_event_initialize, after the whole loop has run. In between:tagsarray, but can be moved by compaction.Either way libyaml could read freed or relocated memory.
This copies the bytes with
ruby_strdupand frees the copies instart_document_ensure, as proposed in the issue. Following the review comment there, the counter is namedn_tagsand is initialized to 0. It is set to the allocated length right afterxcalloc, so the ensure handler frees every slot: slots the loop did not reach are stillNULL, and a handle is not leaked if copying its prefix raises.Two regression tests are added to
test/psych/test_emitter.rb, one per mechanism: handles in ISO-8859-1 emitted underGC.stress, and a handle whoseto_strcompacts the heap. Both fail onmaster(duplicate %TAG directiveandtag handle must not be empty) and pass with this change. The two reproductions from the issue also pass.The compaction test is omitted on the libfyaml backend:
psych_emitter_fy.ckeeps the exported Strings alive in a guard array but still records pointers into them, and this pull request does not change that file.Tested with Ruby 3.4.8 and libyaml 0.2.5 on x86_64-linux; not run on the libfyaml backend, JRuby or TruffleRuby.