8047359: large string size RangeError should be thrown rather than reporting negative length

Reviewed-by: hannesw, attila
This commit is contained in:
Athijegannathan Sundararajan 2014-07-01 17:37:39 +05:30
parent 3e0fd99223
commit 81f8f93225
14 changed files with 69 additions and 11 deletions

@ -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>

@ -152,7 +152,7 @@ class OverloadedMethod {
@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();

@ -111,7 +111,7 @@ public class CompositeTypeBasedGuardingDynamicLinker implements TypeBasedGuardin
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 @@ public class CompositeTypeBasedGuardingDynamicLinker implements TypeBasedGuardin
}
}
@SuppressWarnings("fallthrough")
@Override
protected List<TypeBasedGuardingDynamicLinker> computeValue(final Class<?> clazz) {
List<TypeBasedGuardingDynamicLinker> list = NO_LINKER;

@ -141,7 +141,7 @@ public final class FunctionSignature {
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]));
}
/**

@ -443,7 +443,7 @@ public class NashornClassReader extends ClassReader {
@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;
}

@ -909,6 +909,7 @@ public final class NativeDate extends ScriptObject {
sb.append(n);
}
@SuppressWarnings("fallthrough")
private static String toStringImpl(final Object self, final int format) {
final NativeDate nd = getNativeDate(self);

@ -57,6 +57,9 @@ public final class ConsString implements CharSequence {
this.left = left;
this.right = right;
length = left.length() + right.length();
if (length < 0) {
throw new IllegalArgumentException("too big concatenated String");
}
}
@Override

@ -27,6 +27,7 @@ package jdk.nashorn.internal.runtime;
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 @@ public final class ScriptRuntime {
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);

@ -306,6 +306,7 @@ public final class Source implements Loggable {
return array == null;
}
@SuppressWarnings("try")
protected void checkPermissionAndClose() throws IOException {
try (InputStream in = url.openStream()) {
// empty

@ -201,7 +201,7 @@ public final class UserAccessorProperty extends SpillProperty {
@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 @@ public final class UserAccessorProperty extends SpillProperty {
@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

@ -150,7 +150,7 @@ final class ByteBufferArrayData extends ArrayData {
@Override
public Object getObject(final int index) {
return (int)(0x0ff & buf.get(index));
return 0x0ff & buf.get(index);
}
@Override

@ -90,7 +90,7 @@ public final class InvokeByName {
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);

@ -151,6 +151,7 @@ range.error.invalid.precision=precision argument toPrecision() must be in [1, 21
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

@ -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);
}
}