File tree 2 files changed +22
-1
lines changed 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -47,21 +47,30 @@ var notBacktrack *bitState = nil
47
47
// maxBitStateLen returns the maximum length of a string to search with
48
48
// the backtracker using prog.
49
49
func maxBitStateLen (prog * syntax.Prog ) int {
50
+ if ! shouldBacktrack (prog ) {
51
+ return 0
52
+ }
50
53
return maxBacktrackVector / len (prog .Inst )
51
54
}
52
55
53
56
// newBitState returns a new bitState for the given prog,
54
57
// or notBacktrack if the size of the prog exceeds the maximum size that
55
58
// the backtracker will be run for.
56
59
func newBitState (prog * syntax.Prog ) * bitState {
57
- if len (prog . Inst ) > maxBacktrackProg {
60
+ if ! shouldBacktrack (prog ) {
58
61
return notBacktrack
59
62
}
60
63
return & bitState {
61
64
prog : prog ,
62
65
}
63
66
}
64
67
68
+ // shouldBacktrack reports whether the program is too
69
+ // long for the backtracker to run.
70
+ func shouldBacktrack (prog * syntax.Prog ) bool {
71
+ return len (prog .Inst ) <= maxBacktrackProg
72
+ }
73
+
65
74
// reset resets the state of the backtracker.
66
75
// end is the end position in the input. ncap and reqcap are the number
67
76
// of the machine's capture registers and the number of user-requested
Original file line number Diff line number Diff line change @@ -713,3 +713,15 @@ func TestLongest(t *testing.T) {
713
713
t .Errorf ("longest match was %q, want %q" , g , w )
714
714
}
715
715
}
716
+
717
+ // TestProgramTooLongForBacktrace tests that a regex which is too long
718
+ // for the backtracker still executes properly.
719
+ func TestProgramTooLongForBacktrack (t * testing.T ) {
720
+ longRegex := MustCompile (`(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|twentyone|twentytwo|twentythree|twentyfour|twentyfive|twentysix|twentyseven|twentyeight|twentynine|thirty|thirtyone|thirtytwo|thirtythree|thirtyfour|thirtyfive|thirtysix|thirtyseven|thirtyeight|thirtynine|forty|fortyone|fortytwo|fortythree|fortyfour|fortyfive|fortysix|fortyseven|fortyeight|fortynine|fifty|fiftyone|fiftytwo|fiftythree|fiftyfour|fiftyfive|fiftysix|fiftyseven|fiftyeight|fiftynine|sixty|sixtyone|sixtytwo|sixtythree|sixtyfour|sixtyfive|sixtysix|sixtyseven|sixtyeight|sixtynine|seventy|seventyone|seventytwo|seventythree|seventyfour|seventyfive|seventysix|seventyseven|seventyeight|seventynine|eighty|eightyone|eightytwo|eightythree|eightyfour|eightyfive|eightysix|eightyseven|eightyeight|eightynine|ninety|ninetyone|ninetytwo|ninetythree|ninetyfour|ninetyfive|ninetysix|ninetyseven|ninetyeight|ninetynine|onehundred)` )
721
+ if ! longRegex .MatchString ("two" ) {
722
+ t .Errorf ("longRegex.MatchString(\" two\" ) was false, want true" )
723
+ }
724
+ if longRegex .MatchString ("xxx" ) {
725
+ t .Errorf ("longRegex.MatchString(\" xxx\" ) was true, want false" )
726
+ }
727
+ }
You can’t perform that action at this time.
0 commit comments