Fix String.prototype.indexOf with empty searchString; r=brendan a=blocking-beta9+
authorJan de Mooij <jandemooij@gmail.com>
Wed, 17 Nov 2010 12:21:59 +0100
changeset 58257 13f5b057c586449e954c5f3e34aae46173dac0bc
parent 58256 f6ac6dfa96881dfc819378467952e40d91619519
child 58258 98184990f3ba2cdd38fd505850e17511ca783588
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersbrendan, blocking-beta9
milestone2.0b8pre
Fix String.prototype.indexOf with empty searchString; r=brendan a=blocking-beta9+
js/src/jsstr.cpp
js/src/tests/ecma_5/String/15.5.4.7.js
js/src/tests/ecma_5/String/jstests.list
--- a/js/src/jsstr.cpp
+++ b/js/src/jsstr.cpp
@@ -1460,32 +1460,32 @@ str_indexOf(JSContext *cx, uintN argc, V
 
     jsuint start;
     if (argc > 1) {
         if (vp[3].isInt32()) {
             jsint i = vp[3].toInt32();
             if (i <= 0) {
                 start = 0;
             } else if (jsuint(i) > textlen) {
-                start = 0;
+                start = textlen;
                 textlen = 0;
             } else {
                 start = i;
                 text += start;
                 textlen -= start;
             }
         } else {
             jsdouble d;
             if (!ValueToNumber(cx, vp[3], &d))
                 return JS_FALSE;
             d = js_DoubleToInteger(d);
             if (d <= 0) {
                 start = 0;
             } else if (d > textlen) {
-                start = 0;
+                start = textlen;
                 textlen = 0;
             } else {
                 start = (jsint)d;
                 text += start;
                 textlen -= start;
             }
         }
     } else {
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_5/String/15.5.4.7.js
@@ -0,0 +1,20 @@
+/*
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/
+ */
+
+var BUGNUMBER = 612838;
+var summary = "String.prototype.indexOf with empty searchString";
+
+print(BUGNUMBER + ": " + summary);
+
+assertEq("123".indexOf("", -1), 0);
+assertEq("123".indexOf("", 0), 0);
+assertEq("123".indexOf("", 1), 1);
+assertEq("123".indexOf("", 3), 3);
+assertEq("123".indexOf("", 4), 3);
+assertEq("123".indexOf("", 12345), 3);
+
+reportCompare(true, true);
+
+print("All tests passed!");
--- a/js/src/tests/ecma_5/String/jstests.list
+++ b/js/src/tests/ecma_5/String/jstests.list
@@ -1,3 +1,4 @@
 url-prefix ../../jsreftest.html?test=ecma_5/String/
 script 15.5.4.2.js
+script 15.5.4.7.js
 script 15.5.4.11-01.js