Skip to content

Crash in Java powInt function #261

@mschessler

Description

@mschessler

MathParser.org-mXparser version: v.5.0.4
Framework: Java

Bug:

While fuzzing the library I found multiple inputs crashing it.
All of them have a syntax which passes checkSyntax().
The problem looks to be at org.mariuszgromada.math.mxparser.mathcollection.MathFunctions.powInt(MathFunctions.java:1050).
Take a look at the attached file.

A quick fix would be just catching the exception:

private static double powInt(double a, int n) {
		try {
			if (Double.isNaN(a)) return Double.NaN;
			if (Double.isInfinite(a)) Math.pow(a, n);
			if (a == 0) return Math.pow(a, n);
			if (n == 0) return 1;
			if (n == 1) return a;
			if (mXparser.checkIfCanonicalRounding()) {
				BigDecimal da = BigDecimal.valueOf(a);
				if (n >= 0) return da.pow(n).doubleValue();
				else return BigDecimal.ONE.divide(da, MathContext.DECIMAL128).pow(-n).doubleValue();
			} else {
				return Math.pow(a, n);
			}
		}catch (NumberFormatException | ArithmeticException e){
			return Double.NaN;
		}
}

Exceptions:
Crashes.txt

Just two random inputs which crash:
crash-1a92d270625b937901abb263be8bbd031cab9106.txt
crash-7ecf46c837d7aa976de9844ad1ce9e485b6d2248.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions