8047359: large string size RangeError should be thrown rather than reporting negative length
authorsundar
Tue, 01 Jul 2014 17:37:39 +0530
changeset 25250 b5a4e0ac31d1
parent 25249 919903299b0d
child 25251 eb2479579f1e
8047359: large string size RangeError should be thrown rather than reporting negative length Reviewed-by: hannesw, attila
nashorn/make/build.xml
nashorn/src/jdk/internal/dynalink/beans/OverloadedMethod.java
nashorn/src/jdk/internal/dynalink/support/CompositeTypeBasedGuardingDynamicLinker.java
nashorn/src/jdk/nashorn/internal/codegen/FunctionSignature.java
nashorn/src/jdk/nashorn/internal/ir/debug/NashornClassReader.java
nashorn/src/jdk/nashorn/internal/objects/NativeDate.java
nashorn/src/jdk/nashorn/internal/runtime/ConsString.java
nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java
nashorn/src/jdk/nashorn/internal/runtime/Source.java
nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java
nashorn/src/jdk/nashorn/internal/runtime/linker/InvokeByName.java
nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties
nashorn/test/script/basic/JDK-8047359.js
--- a/nashorn/make/build.xml	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/make/build.xml	Tue Jul 01 17:37:39 2014 +0530
@@ -125,8 +125,7 @@
            encoding="${javac.encoding}"
            includeantruntime="false" fork="true">
       <compilerarg value="-J-Djava.ext.dirs="/>
-      <compilerarg value="-Xlint:unchecked"/>
-      <compilerarg value="-Xlint:deprecation"/>
+      <compilerarg value="-Xlint:all"/>
       <compilerarg value="-XDignore.symbol.file"/>
       <compilerarg value="-Xdiags:verbose"/>
     </javac>
--- a/nashorn/src/jdk/internal/dynalink/beans/OverloadedMethod.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/internal/dynalink/beans/OverloadedMethod.java	Tue Jul 01 17:37:39 2014 +0530
@@ -152,7 +152,7 @@
 
     @SuppressWarnings("unused")
     private MethodHandle selectMethod(final Object[] args) throws NoSuchMethodException {
-        final Class<?>[] argTypes = new Class[args.length];
+        final Class<?>[] argTypes = new Class<?>[args.length];
         for(int i = 0; i < argTypes.length; ++i) {
             final Object arg = args[i];
             argTypes[i] = arg == null ? ClassString.NULL_CLASS : arg.getClass();
--- a/nashorn/src/jdk/internal/dynalink/support/CompositeTypeBasedGuardingDynamicLinker.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/internal/dynalink/support/CompositeTypeBasedGuardingDynamicLinker.java	Tue Jul 01 17:37:39 2014 +0530
@@ -111,7 +111,7 @@
         private final TypeBasedGuardingDynamicLinker[] linkers;
         private final List<TypeBasedGuardingDynamicLinker>[] singletonLinkers;
 
-        @SuppressWarnings("unchecked")
+        @SuppressWarnings(value={"unchecked", "rawtypes"})
         ClassToLinker(final TypeBasedGuardingDynamicLinker[] linkers) {
             this.linkers = linkers;
             singletonLinkers = new List[linkers.length];
@@ -120,6 +120,7 @@
             }
         }
 
+        @SuppressWarnings("fallthrough")
         @Override
         protected List<TypeBasedGuardingDynamicLinker> computeValue(final Class<?> clazz) {
             List<TypeBasedGuardingDynamicLinker> list = NO_LINKER;
--- a/nashorn/src/jdk/nashorn/internal/codegen/FunctionSignature.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/FunctionSignature.java	Tue Jul 01 17:37:39 2014 +0530
@@ -141,7 +141,7 @@
             paramTypeList.add(paramType.getTypeClass());
         }
 
-        this.methodType = MH.type(returnType.getTypeClass(), paramTypeList.toArray(new Class[paramTypes.length]));
+        this.methodType = MH.type(returnType.getTypeClass(), paramTypeList.toArray(new Class<?>[paramTypes.length]));
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/ir/debug/NashornClassReader.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/debug/NashornClassReader.java	Tue Jul 01 17:37:39 2014 +0530
@@ -443,7 +443,7 @@
     @Override
     protected Label readLabel(final int offset, final Label[] labels) {
         final Label label = super.readLabel(offset, labels);
-        label.info = (int)offset;
+        label.info = offset;
         return label;
     }
 
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Tue Jul 01 17:37:39 2014 +0530
@@ -909,6 +909,7 @@
         sb.append(n);
     }
 
+    @SuppressWarnings("fallthrough")
     private static String toStringImpl(final Object self, final int format) {
         final NativeDate nd = getNativeDate(self);
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/ConsString.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ConsString.java	Tue Jul 01 17:37:39 2014 +0530
@@ -57,6 +57,9 @@
         this.left = left;
         this.right = right;
         length = left.length() + right.length();
+        if (length < 0) {
+            throw new IllegalArgumentException("too big concatenated String");
+        }
     }
 
     @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Jul 01 17:37:39 2014 +0530
@@ -27,6 +27,7 @@
 
 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
+import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
 import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
 import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError;
 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
@@ -531,7 +532,11 @@
 
         if (xPrim instanceof String || yPrim instanceof String
                 || xPrim instanceof ConsString || yPrim instanceof ConsString) {
-            return new ConsString(JSType.toCharSequence(xPrim), JSType.toCharSequence(yPrim));
+            try {
+                return new ConsString(JSType.toCharSequence(xPrim), JSType.toCharSequence(yPrim));
+            } catch (final IllegalArgumentException iae) {
+                throw rangeError(iae, "concat.string.too.big");
+            }
         }
 
         return JSType.toNumber(xPrim) + JSType.toNumber(yPrim);
--- a/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Tue Jul 01 17:37:39 2014 +0530
@@ -306,6 +306,7 @@
             return array == null;
         }
 
+        @SuppressWarnings("try")
         protected void checkPermissionAndClose() throws IOException {
             try (InputStream in = url.openStream()) {
                 // empty
--- a/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Tue Jul 01 17:37:39 2014 +0530
@@ -201,7 +201,7 @@
 
     @Override
     public Object getObjectValue(final ScriptObject self, final ScriptObject owner) {
-        return userAccessorGetter(getAccessors((owner != null) ? owner : (ScriptObject)self), self);
+        return userAccessorGetter(getAccessors((owner != null) ? owner : self), self);
     }
 
     @Override
@@ -221,7 +221,7 @@
 
     @Override
     public void setValue(final ScriptObject self, final ScriptObject owner, final Object value, final boolean strict) {
-        userAccessorSetter(getAccessors((owner != null) ? owner : (ScriptObject)self), strict ? getKey() : null, self, value);
+        userAccessorSetter(getAccessors((owner != null) ? owner : self), strict ? getKey() : null, self, value);
     }
 
     @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java	Tue Jul 01 17:37:39 2014 +0530
@@ -150,7 +150,7 @@
 
     @Override
     public Object getObject(final int index) {
-        return (int)(0x0ff & buf.get(index));
+        return 0x0ff & buf.get(index);
     }
 
     @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/linker/InvokeByName.java	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/linker/InvokeByName.java	Tue Jul 01 17:37:39 2014 +0530
@@ -90,7 +90,7 @@
         if(plength == 0) {
             finalPtypes = new Class<?>[] { Object.class, targetClass };
         } else {
-            finalPtypes = new Class[plength + 2];
+            finalPtypes = new Class<?>[plength + 2];
             finalPtypes[0] = Object.class;
             finalPtypes[1] = targetClass;
             System.arraycopy(ptypes, 0, finalPtypes, 2, plength);
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Jun 30 17:31:28 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Tue Jul 01 17:37:39 2014 +0530
@@ -151,6 +151,7 @@
 range.error.invalid.radix=radix argument must be in [2, 36]
 range.error.invalid.date=Invalid Date
 range.error.too.many.errors=Script contains too many errors: {0} errors
+range.error.concat.string.too.big=Concatenated String is too big
 
 reference.error.not.defined="{0}" is not defined
 reference.error.cant.be.used.as.lhs="{0}" can not be used as the left-hand side of assignment
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8047359.js	Tue Jul 01 17:37:39 2014 +0530
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 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-8047359: large string size RangeError should be thrown rather than reporting negative length
+ *
+ * @test
+ * @run
+ */
+
+try {
+    var s = " "; for (var i=0;i<31;++i) s+=s; s.length;
+    throw new Error("should have thrown RangeError!");
+} catch (e) {
+    if (! (e instanceof RangeError)) {
+        fail("RangeError expected, got " + e);
+    }
+}
+
+try {
+    var s = " "; for (var i=0;i<31;++i) s+=s;
+    throw new Error("should have thrown RangeError!");
+} catch (e) {
+    if (! (e instanceof RangeError)) {
+        fail("RangeError expected, got " + e);
+    }
+}