Permalink
Browse files

avoiding infinite loop in String#gsub when there is a zero length match

  • Loading branch information...
1 parent 2a69140 commit 075625c037f1201a227c6ee2ab2b628c25bf8cb1 @rlineweaver committed Oct 31, 2011
Showing with 8 additions and 2 deletions.
  1. +2 −1 src/prototype/lang/string.js
  2. +6 −1 test/unit/string_test.js
@@ -100,7 +100,8 @@ Object.extend(String.prototype, (function() {
}
while (source.length > 0) {
- if (match = source.match(pattern)) {
+ match = source.match(pattern)
+ if (match && match[0].length > 0) {
result += source.slice(0, match.index);
result += String.interpret(replacement(match));
source = source.slice(match.index + match[0].length);
View
@@ -66,7 +66,6 @@ new Test.Unit.Runner({
testGsubWithTroublesomeCharacters: function() {
this.assertEqual('ab', 'a|b'.gsub('|', ''));
- //'ab'.gsub('', ''); // freeze
this.assertEqual('ab', 'ab(?:)'.gsub('(?:)', ''));
this.assertEqual('ab', 'ab()'.gsub('()', ''));
this.assertEqual('ab', 'ab'.gsub('^', ''));
@@ -77,6 +76,12 @@ new Test.Unit.Runner({
this.assertEqual('ab', 'a.b'.gsub('.', ''));
},
+ testGsubWithZeroLengthMatch: function() {
+ this.assertEqual('ab', 'ab'.gsub('', ''));
+ this.assertEqual('a', 'a'.gsub(/b*/, 'c'));
+ this.assertEqual('abc', 'abc'.gsub(/b{0}/, ''));
+ },
+
testSubWithReplacementFunction: function() {
var source = 'foo boo boz';

0 comments on commit 075625c

Please sign in to comment.