0

We have small Sinatra application which runs on REE 1.8.7p-358

NewRelic reports some issues like this:

RegexpError: Stack overflow in regexp matcher: /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/

.../gems/rack-1.4.1/lib/rack/backports/uri/common_18.rb:  67:in `decode_www_form_component'
.../gems/rack-1.4.1/lib/rack/utils.rb:  43:in `unescape'
.../gems/rack-1.4.1/lib/rack/utils.rb:  88:in `parse_nested_query'
.../gems/rack-1.4.1/lib/rack/utils.rb:  88:in `map'
.../gems/rack-1.4.1/lib/rack/utils.rb:  88:in `parse_nested_query'
.../gems/rack-1.4.1/lib/rack/utils.rb:  87:in `each'
.../gems/rack-1.4.1/lib/rack/utils.rb:  87:in `parse_nested_query'
.../gems/rack-1.4.1/lib/rack/request.rb: 334:in `parse_query'
.../gems/rack-1.4.1/lib/rack/request.rb: 209:in `POST'
.../gems/rack-1.4.1/lib/rack/request.rb: 221:in `params'
.../gems/sinatra-1.2.6/lib/sinatra/base.rb: 638:in `call!'
.../gems/sinatra-1.2.6/lib/sinatra/base.rb: 629:in `call'

From Rack sources I see that it uses /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ is used to detect invalid %-encoding:

raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ =~ str

I'd like to try to reproduce the error locally. And my question is: what string should be that to cause stack overflow in regexp matcher on attempt to compare it with /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/

Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
  • That regex it self should not cause such things. (Altho I don't know about the implementation.) Is something filling up the stack before the call maybe? Or maybe something is really broken/corrupted. – Qtax Aug 21 '13 at 15:58
  • 1
    You're using URI, which is a well-tested wheel. I doubt the problem is in URI, but more likely how you're using it. Show us the data you're passing in. Also, it's possible the version you're using is really old and needs bug fixes. – the Tin Man Aug 21 '13 at 16:08
  • There similar issue about this error with regexps: http://stackoverflow.com/questions/4046816/regexperror-stack-overflow-in-regexp-matcher Unfortunately I have not data. That's what I wonder about. – Sergey Potapov Aug 21 '13 at 16:13
  • What's the string are you using against this regex? There's no syntax problem in the regex itself. – NullUserException Aug 21 '13 at 16:27
  • @Qtax: What about catastrophic backtracking? I'm not sure if that's the problem here but it can cause all sorts of unpleasantness. – mu is too short Aug 21 '13 at 18:35
  • @muistooshort, I was talking about this particular regex. With this simple regex there can't be any kind of catastrophic backtracking. The worst time complexity for it is `O(n)`. – Qtax Aug 21 '13 at 21:15
  • I disagree about URI being a well-tested wheel. I've had more than a few issues with it. Also, `[0-9a-fA-F]` is the same as `\h` – pguardiario Aug 21 '13 at 23:25

1 Answers1

-2

You expression /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ is being thrown off if there is more than one %. I been testing it in Rubular

T0ny lombardi
  • 1,800
  • 2
  • 18
  • 35