--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java Mon Dec 15 16:30:45 2014 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java Mon Dec 15 12:08:36 2014 +0100
@@ -275,7 +275,7 @@
final PropertyDescriptor newLenDesc = desc;
// Step 3c and 3d - get new length and convert to long
- final long newLen = NativeArray.validLength(newLenDesc.getValue(), true);
+ final long newLen = NativeArray.validLength(newLenDesc.getValue());
// Step 3e
newLenDesc.setValue(newLen);
@@ -348,8 +348,8 @@
final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length");
// Step 2
- // get old length and convert to long
- final long oldLen = NativeArray.validLength(oldLenDesc.getValue(), true);
+ // get old length and convert to long. Always a Long/Uint32 but we take the safe road.
+ final long oldLen = JSType.toUint32(oldLenDesc.getValue());
// Step 3
if ("length".equals(key)) {
@@ -471,7 +471,7 @@
@Setter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
public static void length(final Object self, final Object length) {
if (isArray(self)) {
- ((ScriptObject)self).setLength(validLength(length, true));
+ ((ScriptObject)self).setLength(validLength(length));
}
}
@@ -495,18 +495,13 @@
length(self, length); // Same as instance setter but we can't make nasgen use the same method for prototype
}
- static long validLength(final Object length, final boolean reject) {
+ static long validLength(final Object length) {
+ // ES5 15.4.5.1, steps 3.c and 3.d require two ToNumber conversions here
final double doubleLength = JSType.toNumber(length);
- if (!Double.isNaN(doubleLength) && JSType.isRepresentableAsLong(doubleLength)) {
- final long len = (long) doubleLength;
- if (len >= 0 && len <= JSType.MAX_UINT) {
- return len;
- }
- }
- if (reject) {
+ if (doubleLength != JSType.toUint32(length)) {
throw rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length));
}
- return -1;
+ return (long) doubleLength;
}
/**
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeRegExpExecResult.java Mon Dec 15 16:30:45 2014 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeRegExpExecResult.java Mon Dec 15 12:08:36 2014 +0100
@@ -88,7 +88,7 @@
@Setter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
public static void length(final Object self, final Object length) {
if (self instanceof ScriptObject) {
- ((ScriptObject)self).setLength(NativeArray.validLength(length, true));
+ ((ScriptObject)self).setLength(NativeArray.validLength(length));
}
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066215.js Mon Dec 15 12:08:36 2014 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8066215: Fuzzing bug: length valueOf bug
+ *
+ * @test
+ * @run
+ */
+
+function defineLength(arr, length) {
+ Object.defineProperty(arr, "length", {
+ value: {
+ valueOf: function() {
+ print("value retrieved: " + length);
+ return length;
+ }
+ }
+ });
+ print("done: " + arr.length + ", " + typeof arr.length);
+}
+
+var a = [];
+defineLength(a, 3);
+defineLength(a, 6);
+defineLength(a, 3);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066215.js.EXPECTED Mon Dec 15 12:08:36 2014 +0100
@@ -0,0 +1,9 @@
+value retrieved: 3
+value retrieved: 3
+done: 3, number
+value retrieved: 6
+value retrieved: 6
+done: 6, number
+value retrieved: 3
+value retrieved: 3
+done: 3, number