Skip to content

Commit

Permalink
Fix problem which causes regular expression matching to become
Browse files Browse the repository at this point in the history
exponentially slower on longer email addresses.
  • Loading branch information
nullterminated committed Feb 14, 2013
1 parent 161caca commit ecfa4cf
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ public final class ERXEmailValidator implements Serializable {
private final String mailbox;
private final String patternString;
private final Pattern validPattern;

/**
* This second validator exists because there is an issue with validating
* addresses that allowQuotedIdentifiers that have no quoting and a long
* mailbox name. Example: [email protected]
*
* It seems that after about 25 chars, the regular expression matching
* takes exponentially longer to match the string. The same address with
* quoting does not exhibit the problem.
* Ex. "Blah blah" <[email protected]>
*
* Nor does using a validator that does not allow quoted identifiers. In
* order to work around this problem, a second internal validator is
* created when allowQuotedIdentifiers is true. This internal validator
* does not allow quoted identifiers. It is tried first and only if it
* returns false is the full regular expression used.
*/
private final ERXEmailValidator _internal;

/**
*
Expand Down Expand Up @@ -128,6 +146,11 @@ public ERXEmailValidator(boolean allowQuotedIdentifiers, boolean allowDomainLite
mailbox = nameAddr + "|" + addrSpec;
patternString = allowQuotedIdentifiers ? mailbox : addrSpec;
validPattern = Pattern.compile(patternString);

/*
* See javadoc for the _internal ivar
*/
_internal = allowQuotedIdentifiers?new ERXEmailValidator(false, allowDomainLiterals):null;
}

/**
Expand All @@ -140,6 +163,12 @@ public ERXEmailValidator(boolean allowQuotedIdentifiers, boolean allowDomainLite
* otherwise.
*/
public boolean isValidEmailString(String email) {
/*
* See javadoc for the _internal ivar
*/
if(_internal != null && _internal.isValidEmailString(email)) {
return true;
}
return email != null && validPattern.matcher(email).matches();
}

Expand Down

0 comments on commit ecfa4cf

Please sign in to comment.