Skip to content

Commit

Permalink
fix minor issues with error trapping -- some scenarios can result in …
Browse files Browse the repository at this point in the history
…an ArrayIndexOutOfBounds when writing out an error.
  • Loading branch information
mikebrock committed Oct 7, 2012
1 parent 9477597 commit 69bd7c8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 62 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/mvel2/PropertyAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private Object get() {
throw new PropertyAccessException("could not access property", property, cursor, e);
}
catch (IndexOutOfBoundsException e) {
if (cursor >= length) cursor = length -1;

throw new PropertyAccessException("array or collections index out of bounds in property: "
+ new String(property, cursor, length), property, cursor, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ public CachingMapVariableResolverFactory(Map variables) {
this.variables = variables;
}

public CachingMapVariableResolverFactory(Map<String, Object> variables, VariableResolverFactory nextFactory) {
this.variables = variables;
this.nextFactory = nextFactory;
}

public CachingMapVariableResolverFactory(Map<String, Object> variables, boolean cachingSafe) {
this.variables = variables;
}

public VariableResolver createVariable(String name, Object value) {
VariableResolver vr;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/mvel2/util/ErrorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
public class ErrorUtil {
public static CompileException rewriteIfNeeded(CompileException caught, char[] outer, int outerCursor) {
if (outer != caught.getExpr()) {
if (caught.getExpr().length <= caught.getCursor()) {
caught.setCursor(caught.getExpr().length - 1);
}

try {
String innerExpr = new String(caught.getExpr()).substring(caught.getCursor());
caught.setExpr(outer);

Expand All @@ -18,6 +23,10 @@ public static CompileException rewriteIfNeeded(CompileException caught, char[] o
.indexOf(innerExpr);

caught.setCursor(newCursor);
}
catch (Throwable t) {
t.printStackTrace();
}
}
return caught;
}
Expand Down
50 changes: 0 additions & 50 deletions src/main/java/org/mvel2/util/ParseTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -1077,56 +1077,6 @@ public static int __resolveType(Class cls) {
}
}
return code;


// if (Integer.class == cls)
// return DataTypes.W_INTEGER;
// if (Double.class == cls)
// return DataTypes.W_DOUBLE;
// if (Boolean.class == cls)
// return DataTypes.W_BOOLEAN;
// if (String.class == cls)
// return DataTypes.STRING;
// if (Long.class == cls)
// return DataTypes.W_LONG;
//
// if (Short.class == cls)
// return DataTypes.W_SHORT;
// if (Float.class == cls)
// return DataTypes.W_FLOAT;
//
// if (Byte.class == cls)
// return DataTypes.W_BYTE;
// if (Character.class == cls)
// return DataTypes.W_CHAR;
//
// if (BigDecimal.class == cls)
// return DataTypes.BIG_DECIMAL;
//
// if (BigInteger.class == cls)
// return DataTypes.BIG_INTEGER;
//
// if (int.class == cls)
// return INTEGER;
// if (short.class == cls)
// return DataTypes.SHORT;
// if (float.class == cls)
// return DataTypes.FLOAT;
// if (double.class == cls)
// return DOUBLE;
// if (long.class == cls)
// return LONG;
// if (boolean.class == cls)
// return DataTypes.BOOLEAN;
// if (byte.class == cls)
// return DataTypes.BYTE;
// if (char.class == cls)
// return DataTypes.CHAR;
//
// if (BlankLiteral.class == cls)
// return DataTypes.EMPTY;

// return DataTypes.OBJECT;
}

public static boolean isNumericallyCoercible(Class target, Class parm) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/mvel2/util/PropertyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public static Method getSetter(Class clazz, String property, Class type) {

for (Method meth : clazz.getMethods()) {
if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 1 &&
(property.equals(meth.getName()) || simple.equals(meth.getName())) && (type == null || canConvert(meth.getParameterTypes()[0], type))) {
(property.equals(meth.getName()) || simple.equals(meth.getName()))
&& (type == null || canConvert(meth.getParameterTypes()[0], type))) {
return meth;
}
}
Expand All @@ -96,7 +97,8 @@ public static Method getGetter(Class clazz, String property) {
Method candidate = null;
for (Method meth : clazz.getMethods()) {
if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 0
&& (property.equals(meth.getName()) || (isGet.equals(meth.getName()) && meth.getReturnType() == boolean.class) || simple.equals(meth.getName()))) {
&& (property.equals(meth.getName()) || (isGet.equals(meth.getName()) && meth.getReturnType() == boolean.class)
|| simple.equals(meth.getName()))) {
if (candidate == null || candidate.getReturnType().isAssignableFrom(meth.getReturnType())) {
candidate = meth;
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/mvel2/tests/core/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.mvel2.ast.Function;
import org.mvel2.compiler.CompiledExpression;
import org.mvel2.compiler.ExpressionCompiler;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;
import org.mvel2.optimizers.OptimizerFactory;
import org.mvel2.util.CompilerTools;
import org.mvel2.util.MVELClassLoader;

import static org.mvel2.util.CompilerTools.extractAllDeclaredFunctions;

Expand Down Expand Up @@ -186,4 +188,16 @@ public void testFunctionSemantics() {
}


public void testFunctionReuse() {
VariableResolverFactory functionFactory = new MapVariableResolverFactory();
MVEL.eval("def foo() { \"foo\"; }; def bar() { \"bar\" };", functionFactory);

VariableResolverFactory myVarFactory = new MapVariableResolverFactory();
myVarFactory.setNextFactory(functionFactory);

Serializable s = MVEL.compileExpression("foo() + bar();");

assertEquals("foobar", MVEL.executeExpression(s, myVarFactory));
}

}
6 changes: 5 additions & 1 deletion src/test/java/org/mvel2/tests/fuzz/Fuzzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public static void main(String[] args) throws IOException {
}

if (run % 25000 == 0 && run != 0) {
rate = run / (time = (currentTimeMillis() - start) / 1000);
long l = time = (currentTimeMillis() - start) / 1000;
if (l == 0) {
l = 1;
}
rate = run / l;
System.out.println("Run: " + df.format(run) + " times; "
+ df.format(time) + "secs; " + df.format(rate) + " avg. per second.");
}
Expand Down

0 comments on commit 69bd7c8

Please sign in to comment.