asciidoctor / asciimath

Asciimath parser
MIT License
24 stars 16 forks source link

asciidoctor-pdf fails with "stack level too deep" for certain expressions #62

Closed SanchithHegde closed 3 years ago

SanchithHegde commented 3 years ago

First of all, I'm new to the ruby ecosystem so I'm sorry if the fix for this issue is trivial or if I should have reported this issue elsewhere. If that is indeed the case, please let me know which repo I should be filing this issue under and I'll happily do so.


Steps to reproduce

Contents of test.adoc file (the actual expressions that caused the issue are slightly more complex, these are just the minimal expressions with which I could reproduce it):

:stem: asciimath

asciimath:[R(alpha_(K+1)|x)]

asciimath:[x ((y) ^ (1-x))]

asciidoctor-pdf invocation

asciidoctor-pdf --trace --require asciidoctor-mathematical test.adoc

Expected result

The expression gets parsed and rendered without issues.

Actual result

asciidoctor-pdf fails with "stack level too deep".

Trace ``` Traceback (most recent call last): 9775: from /home/username/.asdf/installs/ruby/2.7.3/bin/asciidoctor-pdf:23:in `
' 9774: from /home/username/.asdf/installs/ruby/2.7.3/bin/asciidoctor-pdf:23:in `load' 9773: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-pdf-1.6.0/bin/asciidoctor-pdf:27:in `' 9772: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-2.0.15/lib/asciidoctor/cli/invoker.rb:113:in `invoke!' 9771: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-2.0.15/lib/asciidoctor/cli/invoker.rb:113:in `each' 9770: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-2.0.15/lib/asciidoctor/cli/invoker.rb:130:in `block in invoke!' 9769: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-2.0.15/lib/asciidoctor/convert.rb:189:in `convert_file' 9768: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciidoctor-2.0.15/lib/asciidoctor/convert.rb:189:in `open' ... 9763 levels... 4: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciimath-2.0.2/lib/asciimath/latex.rb:385:in `block in is_very_small' 3: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciimath-2.0.2/lib/asciimath/latex.rb:385:in `is_very_small' 2: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciimath-2.0.2/lib/asciimath/latex.rb:385:in `all?' 1: from /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciimath-2.0.2/lib/asciimath/ast.rb:99:in `each' /home/username/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/asciimath-2.0.2/lib/asciimath/ast.rb:99:in `each': stack level too deep (SystemStackError) ```

Remarks

Each of the expressions can independently cause the issue.

With the first one, I won't get the error if the expression is either: alpha_(K+1)|x (adding parentheses around the expression throws the error), or R(alpha_(K)|x) (adding any operator after K throws the error).

Similar results with the second one, (y) ^ (1-x) and x ((y) ^ (x)) don't throw any errors.

I was also able to reproduce this issue with the docker-asciidoctor container if that's of any help.


If there's any more information required from my end, I'll gladly provide.

pepijnve commented 3 years ago

@SanchithHegde I've already found the cause of the 'stack level too deep' error and fixed that. The input R(alpha_(K+1)|x) also revealed a parsing error. Still working on fixing that.

SanchithHegde commented 3 years ago

@pepijnve That's great! Thank you!

am21 commented 3 years ago

Found that simple things like ceil(-2.5), floor(-oo) give the same error, where abs(-2.5) works.

I applied the patch manually in the local gem and it fixed it.

pepijnve commented 3 years ago

I've logged #64 to track the | parsing issue. I'll make a patch release containing the bugfix for the infinite recursion already.