Skip to content

Commit

Permalink
small defensive programming
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed May 21, 2024
1 parent 29c19c9 commit 9d6dbbc
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@
*/
package ortus.boxlang.runtime.bifs.global.math;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@BoxBIF
public class PrecisionEvaluate extends BIF {

/**
* Our expression regex parser
*/
private static final Pattern pattern = Pattern.compile( "^[0-9+\\-*/^%\\\\()\\s]*(MOD\\s*)?[0-9+\\-*/^%\\\\()\\s]*$" );

/**
* Constructor
*/
Expand All @@ -55,14 +61,19 @@ public PrecisionEvaluate() {
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
String expressions = arguments.getAsString( Key.expressions );
String regex = "^[0-9+\\-*/^%\\\\()\\s]*(MOD\\s*)?[0-9+\\-*/^%\\\\()\\s]*$";
Pattern pattern = Pattern.compile( regex );
Matcher matcher = pattern.matcher( expressions );
// make sure we are maths before we execute to stop any bad actors
if ( matcher.matches() ) {
Double results = ( double ) runtime.executeStatement( expressions, context );
BigDecimal finalResults = BigDecimal.valueOf( results );
return finalResults;
Double results;
try {
results = ( double ) runtime.executeStatement( expressions, context );
} catch ( Exception e ) {
throw new BoxRuntimeException(
"Error evaluating expression: " + e.getMessage(),
e
);
}
return BigDecimal.valueOf( results );
} else {
throw new BoxRuntimeException( "The expressions provided are not valid" );
}
Expand Down

0 comments on commit 9d6dbbc

Please sign in to comment.