Skip to content
Tcl Source Code View Ticket
Login

View Ticket

[regexp -all {a*} a] loops infinitely

Closed / Fixed219073ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:10:19
OriginalBugID: 4981 Bug
Version: 8.3
SubmitDate: '2000-04-10'
LastModified: '2000-04-10'
Severity: CRIT
Status: Released
Submitter: ericm
ChangedBy: ericm
OS: All
FixedDate: '2000-04-10'
FixedInVersion: 8.3.1
ClosedDate: '2000-10-25'


Because of the implementation of regexp -all, this command:

regexp -all {a*} a

will loop indefinately.  The problem is traced to the regexp command, in which the following (essentially) occurs:

while (1) {
matchRegexp(...offset...);

...

offset += matchLength;
}

In the case of {a*} and the string a, the regexp will match the {} at the end of the string.  Of course, the length of {} is 0, so the offset never gets incremented, so the next pass through the loop matches against the same chunk of the string.  And so on, ad infinitum.

The fix requires two changes:

1.  offset should always be incremented by min(1, matchLength)
2.  after offset has been incremented, it should be tested to see if it is past the end of the string; if so, the loop should terminate.  the regexp matching engine has the invarient that starting indices past the end of the string get confined to the range of the string, so specifying an offset of stringLength+1 will not cause it to fail to match -- it will just interpret that as stringLength.

- eric



Fixed for 8.3.1., with tests.

- eric
welch Closed ticket [219073ffff]. [History diff]2000-10-26 08:34:07
Log in (anonymous is fine) to add a comment.