Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid invalid length when building name symbol (#348) #349

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/ox/sax.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ static char read_element_end(SaxDrive dr) {
} else {
snprintf(msg, sizeof(msg) - 1, "%selement '%s' closed but not opened", EL_MISMATCH, dr->buf.str);
ox_sax_drive_error_at(dr, msg, pos, line, col);
name = str2sym(dr, dr->buf.str, dr->buf.tail - dr->buf.str - 2, 0);
name = str2sym(dr, dr->buf.str, strlen(dr->buf.str), 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out, dr->buf.str may not be null terminated which will give an incorrect length. A different approach will be needed to handle when the end tag is empty if the existing code fails. It seems like it should work though.

if (dr->has_start_element && 0 >= dr->blocked &&
(NULL == h || ActiveOverlay == h->overlay || NestOverlay == h->overlay)) {
VALUE args[1];
Expand Down
20 changes: 20 additions & 0 deletions test/sax/sax_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,26 @@ def test_sax_non_utf8_encoding
end
end

def test_sax_last_element_closed_not_opened
Ox.default_options = $ox_sax_options
parse_compare(%{</top>},
[
[:error, "Start End Mismatch: element 'top' closed but not opened", 1, 1],
[:start_element, :top],
[:end_element, :top]
])
end

def test_sax_last_unnamed_element_closed_not_opened
Ox.default_options = $ox_sax_options
parse_compare(%{</>},
[
[:error, "Start End Mismatch: element '' closed but not opened", 1, 1],
[:start_element, :''],
[:end_element, :'']
])
end

def test_sax_value_fixnum
Ox.default_options = $ox_sax_options
handler = TypeSax.new(:as_i)
Expand Down