Skip to content

Commit

Permalink
Merge pull request #75 from arthurscchan/regex-limit
Browse files Browse the repository at this point in the history
Add limit for regex length
  • Loading branch information
enebo authored Aug 9, 2023
2 parents 4fdf926 + 5cd8965 commit c187bb2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/org/joni/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.PrintStream;

public interface Config extends org.jcodings.Config {
final int REGEX_MAX_LENGTH = ConfigSupport.getInt("joni.regex_max_length", 10000);
final int CHAR_TABLE_SIZE = ConfigSupport.getInt("joni.char_table_size", 256);
final boolean USE_NO_INVALID_QUANTIFIER = ConfigSupport.getBoolean("joni.use_no_invalid_quantifier", true);
final int SCANENV_MEMNODES_SIZE = ConfigSupport.getInt("joni.scanenv_memnodes_size", 8);
Expand Down
4 changes: 4 additions & 0 deletions src/org/joni/Regex.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.joni.exception.ErrorMessages;
import org.joni.exception.InternalException;
import org.joni.exception.ValueException;
import org.joni.Config;

public final class Regex {
int[] code; /* compiled pattern */
Expand Down Expand Up @@ -150,6 +151,9 @@ public Regex(byte[]bytes, int p, int end, int option, Encoding enc, Syntax synta

// onig_alloc_init
public Regex(byte[]bytes, int p, int end, int option, int caseFoldFlag, Encoding enc, Syntax syntax, WarnCallback warnings) {
if ((end - p) > Config.REGEX_MAX_LENGTH) {
throw new ValueException(ErrorMessages.REGEX_TOO_LONG);
}

if ((option & (Option.DONT_CAPTURE_GROUP | Option.CAPTURE_GROUP)) ==
(Option.DONT_CAPTURE_GROUP | Option.CAPTURE_GROUP)) {
Expand Down
1 change: 1 addition & 0 deletions src/org/joni/exception/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface ErrorMessages extends org.jcodings.exception.ErrorMessages {
final String INVALID_ARGUMENT = "invalid argument";

/* syntax error */
final String REGEX_TOO_LONG = "regex length too long";
final String END_PATTERN_AT_LEFT_BRACE = "end pattern at left brace";
final String END_PATTERN_AT_LEFT_BRACKET = "end pattern at left bracket";
final String EMPTY_CHAR_CLASS = "empty char-class";
Expand Down

0 comments on commit c187bb2

Please sign in to comment.