Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix the email regexp CPU usage for odd inputs #36

Merged
merged 1 commit into from

4 participants

@mitio

Fixes #34

Some inputs could make the regexp engine in Ruby take up a lot of CPU time in order to check for a match of an email in the input string. This change adds a test to guard against an example of such an input and changes the email regexp so that all the tests pass and so that no excessive CPU usage occurs.

The new email regexp is based on the changes suggested by @andresbravog in #34 but is changed a bit so that tests pass.

@mitio mitio Fix the email regexp CPU usage for odd inputs
Fixes #34

Some inputs could make the Regexp engine in Ruby to take up a lot of
CPU time in order to check for a match of the email string. This change
adds a test to guard against an example of such an input and changes
the email regexp so that all the tests pass and so that no excessive
CPU usage occurs.
759f05b
@xuanxu xuanxu merged commit 33193b8 into tenderlove:master
@mitio mitio deleted the mitio:fix-greedy-email-regexp branch
@ktdreyer

Please see also #40 for a fix when running the tests outside of Rake or Bundler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 22, 2013
  1. @mitio

    Fix the email regexp CPU usage for odd inputs

    mitio authored
    Fixes #34
    
    Some inputs could make the Regexp engine in Ruby to take up a lot of
    CPU time in order to check for a match of the email string. This change
    adds a test to guard against an example of such an input and changes
    the email regexp so that all the tests pass and so that no excessive
    CPU usage occurs.
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 1 deletion.
  1. +1 −1  lib/rails_autolink/helpers.rb
  2. +14 −0 test/test_rails_autolink.rb
View
2  lib/rails_autolink/helpers.rb
@@ -79,7 +79,7 @@ def auto_link(text, *args, &block) #link = :all, html = {}, &block)
AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i]
AUTO_EMAIL_LOCAL_RE = /[\w.!#\$%&'*\/=?^`{|}~+-]/
- AUTO_EMAIL_RE = /[\w.!#\$%+-]\.?(?:#{AUTO_EMAIL_LOCAL_RE}+\.)*#{AUTO_EMAIL_LOCAL_RE}*@[\w-]+(?:\.[\w-]+)+/
+ AUTO_EMAIL_RE = /[\w.!#\$%+-]\.?#{AUTO_EMAIL_LOCAL_RE}*@[\w-]+(?:\.[\w-]+)+/
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
View
14 test/test_rails_autolink.rb
@@ -309,6 +309,20 @@ def test_auto_link_parsing
end
end
+ def test_auto_link_does_not_timeout_when_parsing_odd_email_input
+ inputs = %w(
+ foo@...................................
+ foo@........................................
+ foo@.............................................
+ )
+
+ inputs.each do |input|
+ Timeout.timeout(0.2) do
+ assert_equal input, auto_link(input)
+ end
+ end
+ end
+
private
def generate_result(link_text, href = nil, escape = false)
href ||= link_text
Something went wrong with that request. Please try again.