Skip to content

Commit

Permalink
Don't SEGV if user returns a non string object
Browse files Browse the repository at this point in the history
Make sure the object we get back on the reader function is actually a
string.  If it isn't, return an error to libxml2.

Fixes #898
  • Loading branch information
tenderlove committed Feb 20, 2018
1 parent 4f9bfd1 commit a24b92e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/nokogiri/xml_io.c
Expand Up @@ -21,6 +21,7 @@ int io_read_callback(void * ctx, char * buffer, int len) {

if (NIL_P(string)) return 0;
if (string == Qundef) return -1;
if (TYPE(string) != T_STRING) return -1;

str_len = (size_t)RSTRING_LEN(string);
safe_len = str_len > (size_t)len ? (size_t)len : str_len;
Expand Down
12 changes: 12 additions & 0 deletions test/xml/test_reader.rb
Expand Up @@ -4,6 +4,18 @@
module Nokogiri
module XML
class TestReader < Nokogiri::TestCase
class NonStringIO
def read(size)
:invalid_object
end
end

def test_io_non_string
io = NonStringIO.new
reader = Nokogiri::XML::Reader(io)
assert_equal io, reader.source
end

def test_from_io_sets_io_as_source
io = File.open SNUGGLES_FILE
reader = Nokogiri::XML::Reader.from_io(io)
Expand Down

0 comments on commit a24b92e

Please sign in to comment.