8007522: IllegalStateException thrown from String.prototype.search function
authorsundar
Tue, 05 Feb 2013 21:00:04 +0530
changeset 16214 299e3d0ec77e
parent 16213 66cb726a76d5
child 16215 92f00bfd6059
8007522: IllegalStateException thrown from String.prototype.search function Reviewed-by: jlaskey
nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java
nashorn/test/script/basic/JDK-8007522.js
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java	Tue Feb 05 18:44:54 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java	Tue Feb 05 21:00:04 2013 +0530
@@ -675,7 +675,7 @@
         start = matcher.find(start) ? matcher.start() : -1;
 
         if (global) {
-            setLastIndex(matcher.end());
+            setLastIndex(start == -1? -1 : matcher.end());
         }
 
         return start;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8007522.js	Tue Feb 05 21:00:04 2013 +0530
@@ -0,0 +1,13 @@
+/**
+ * JDK-8007522: IllegalStateException thrown from String.prototype.search function
+ *
+ * @test
+ * @run
+ */
+
+var str = "hello";
+// search used to result in IllegalStateException
+if (str.search(/foo/g) != -1) {
+    fail("String.prototype.search failed");
+}
+