Repository navigation
Fix an exception from the SAX input IO coming back as a TypeError - #476
Merged
Merged
Conversation
rescue_cb() is the rb_rescue handler for the read()/readpartial() calls Ox.sax_parse makes on an IO-like input. rb_rescue passes the handler the exception instance, but it handed that instance to rb_raise(), which expects a class, so Ruby raised a TypeError about the argument type while building the new exception and the error the IO raised was lost. A stream wrapper enforcing a size limit, or an IO failing mid-read, had to stash the error and re-raise it after sax_parse returned. Re-raise the original error with the position appended to its message. Exception#exception clones the receiver and swaps the message in, so the class survives whatever initialize() it defines; rb_exc_new_str() would call .new(msg) and break on a zero-argument one. TypeError was in the swallow list only because IO#read(len) returns nil at EOF and StringValuePtr() raises TypeError on nil, so a TypeError raised inside read() for any other reason ended the parse silently. io_cb() and partial_io_cb() now test NIL_P() and report end of input directly, and the handler no longer catches TypeError. EOFError still ends the parse, which is how readpartial() signals it. Raising from the handler is safe: ox_sax_drive_cleanup() runs after the rb_protect() in sax_parse() catches it. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Owner
|
Thank you. |
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.
rescue_cb() is the rb_rescue handler for the read()/readpartial() calls Ox.sax_parse makes on an IO-like input. rb_rescue passes the handler the exception instance, but it handed that instance to rb_raise(), which expects a class, so Ruby raised a TypeError about the argument type while building the new exception and the error the IO raised was lost. A stream wrapper enforcing a size limit, or an IO failing mid-read, had to stash the error and re-raise it after sax_parse returned.
Re-raise the original error with the position appended to its message. Exception#exception clones the receiver and swaps the message in, so the class survives whatever initialize() it defines; rb_exc_new_str() would call .new(msg) and break on a zero-argument one.
TypeError was in the swallow list only because IO#read(len) returns nil at EOF and StringValuePtr() raises TypeError on nil, so a TypeError raised inside read() for any other reason ended the parse silently. io_cb() and partial_io_cb() now test NIL_P() and report end of input directly, and the handler no longer catches TypeError. EOFError still ends the parse, which is how readpartial() signals it.
Raising from the handler is safe: ox_sax_drive_cleanup() runs after the rb_protect() in sax_parse() catches it.