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

Invalid name for an orphan end tag #348

Open
albanpeignier opened this issue Feb 27, 2024 · 0 comments · May be fixed by #349
Open

Invalid name for an orphan end tag #348

albanpeignier opened this issue Feb 27, 2024 · 0 comments · May be fixed by #349

Comments

@albanpeignier
Copy link

albanpeignier commented Feb 27, 2024

In some conditions, a final orphan end tag is reported to the Sax handler incorrectly:

➜ echo -n "</root>" | bundle exec test.rb
start_element: 'roo'
end_element: 'roo'

Even worse, when the orphan end tag is empty, the name can't be built and causes this error:

➜ echo -n "</>" | bundle exec test.rb
bundler: failed to load command: test.rb (test.rb)
test.rb:10:in `sax_parse': negative string size (or size too big) (ArgumentError)

Ox.sax_parse(Handler.new, StringIO.new(STDIN.read))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        from test.rb:10:in `<top (required)>'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli/exec.rb:58:in `load'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli/exec.rb:58:in `kernel_load'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli/exec.rb:23:in `run'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli.rb:451:in `exec'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli.rb:34:in `dispatch'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/cli.rb:28:in `start'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/exe/bundle:28:in `block in <top (required)>'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
        from /home/user/Projects/ox-test/vendor/bundle/ruby/3.3.0/gems/bundler-2.5.6/exe/bundle:20:in `<top (required)>'
        from /home/user/.rbenv/versions/3.3.0/bin/bundle:25:in `load'
        from /home/user/.rbenv/versions/3.3.0/bin/bundle:25:in `<main>

➜ echo -n "<root></root></>" | bundle exec test.rb
start_element: 'root'
end_element: 'root'
bundler: failed to load command: test.rb (test.rb)
test.rb:10:in `sax_parse': negative string size (or size too big) (ArgumentError)
...

This problem doesn't occur when the XML payload contains any character after the end tag:

➜ echo -n "</> " | bundle exec test.rb
start_element: ''
end_element: ''

➜ echo "</>" | bundle exec test.rb
start_element: ''
end_element: ''

➜ echo "</root>" | bundle exec test.rb
start_element: 'root'
end_element: 'root'

Context

  • Ox: 2.14.17
  • Ruby: 3.3 or 2.7.8

The tiny test.rb used to parse the given input:

#!/usr/bin/env ruby

require 'ox'

class Handler
  def start_element(name); puts "start_element: '#{name}'"; end
  def end_element(name); puts "end_element: '#{name}'"; end
end

Ox.sax_parse(Handler.new, StringIO.new(STDIN.read))
albanpeignier pushed a commit to albanpeignier/ox that referenced this issue Mar 3, 2024
Use strlen instead of calculation on dr->buf to manage both strings with and
without \0 last character.

Add tests on last element closed not opened (aka orphan).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant