Skip to content

Commit

Permalink
resolves asciidoctor#4368 redo loop rather than using recursion to lo…
Browse files Browse the repository at this point in the history
…cate next line to process
  • Loading branch information
mojavelinux committed Oct 17, 2022
1 parent 5c8afc1 commit 0c7aa83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Expand Up @@ -57,6 +57,10 @@ Improvements::
* Set linenums option on source block when line numbering is enabled (#3313)
* Warn if include target is remote and `allow-uri-read` attribute is not set (#2284)

Bug Fixes::

* Redo loop rather than using recursion to locate next line to process; prevents stack limit error (#4368)

== 2.0.18 (2022-10-15) - @mojavelinux

Improvements::
Expand Down
14 changes: 7 additions & 7 deletions lib/asciidoctor/reader.rb
Expand Up @@ -129,16 +129,16 @@ def next_line_empty?
# Returns the next line of the source data as a String if there are lines remaining.
# Returns nothing if there is no more data.
def peek_line direct = false
if direct || @look_ahead > 0
@unescape_next_line ? ((line = @lines[-1]).slice 1, line.length) : @lines[-1]
elsif @lines.empty?
@look_ahead = 0
nil
else
while true
return @unescape_next_line ? ((line = @lines[-1]).slice 1, line.length) : @lines[-1] if direct || @look_ahead > 0
if @lines.empty?
@look_ahead = 0
return
end
# FIXME the problem with this approach is that we aren't
# retaining the modified line (hence the @unescape_next_line tweak)
# perhaps we need a stack of proxied lines
(process_line @lines[-1]) || peek_line
return (process_line @lines[-1]) || redo
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/reader_test.rb
Expand Up @@ -2819,6 +2819,24 @@ def process reader, target, attributes
assert_empty logger
end
end

test 'should not fail to process preprocessor directive that evaluates to false and has a large number of lines' do
lines = (%w(data) * 5000) * ?\n
input = <<~EOS
before
ifdef::attribute-not-set[]
#{lines}
endif::attribute-not-set[]
after
EOS

doc = Asciidoctor.load input
assert_equal 2, doc.blocks.size
assert_equal 'before', doc.blocks[0].source
assert_equal 'after', doc.blocks[1].source
end
end
end
end

0 comments on commit 0c7aa83

Please sign in to comment.