Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public boolean enableHeterogeneousNumericComparisons() {

public abstract boolean enableIeee754ContainerEquality();

public abstract boolean enableExactDoubleToIntRange();

public abstract int maxRegexProgramSize();

public abstract Builder toBuilder();
Expand Down Expand Up @@ -175,6 +177,7 @@ public static Builder newBuilder() {
.enableComprehension(true)
.enableTimestampOverflowCheck(true)
.enableIeee754ContainerEquality(true)
.enableExactDoubleToIntRange(true)
.maxRegexProgramSize(-1);
}

Expand Down Expand Up @@ -546,6 +549,12 @@ public Builder enableHeterogeneousNumericComparisons(boolean value) {
*/
public abstract Builder enableIeee754ContainerEquality(boolean value);

/**
* Converts {@code -9223372036854775808.0} (-2^63) to the minimum int in {@code int(double)}
* (enabled by default).
*/
public abstract Builder enableExactDoubleToIntRange(boolean value);

public abstract CelOptions build();
}
}
1 change: 1 addition & 0 deletions common/src/test/java/dev/cel/common/CelOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void current_defaults() {
assertThat(CelOptions.current().build().enableUnknownTracking()).isFalse();
assertThat(CelOptions.current().build().enablePrattParser()).isFalse();
assertThat(CelOptions.current().build().enableIeee754ContainerEquality()).isTrue();
assertThat(CelOptions.current().build().enableExactDoubleToIntRange()).isTrue();
}

@Test
Expand Down
29 changes: 15 additions & 14 deletions runtime/src/main/java/dev/cel/runtime/standard/IntFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,21 @@ public enum IntOverload implements CelStandardOverload {
});
}
}),
DOUBLE_TO_INT64(
(celOptions, runtimeEquality) ->
CelFunctionBinding.from(
"double_to_int64",
Double.class,
(Double arg) -> {
if (celOptions.errorOnIntWrap()) {
return RuntimeHelpers.doubleToLongChecked(arg)
.orElseThrow(
() ->
new CelNumericOverflowException("double is out of range for int"));
}
return arg.longValue();
})),
DOUBLE_TO_INT64((celOptions, runtimeEquality) ->
CelFunctionBinding.from(
"double_to_int64",
Double.class,
(Double arg) -> {
if (celOptions.errorOnIntWrap()) {
if (celOptions.enableExactDoubleToIntRange() && arg == (double) Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
return RuntimeHelpers.doubleToLongChecked(arg)
.orElseThrow(
() -> new CelNumericOverflowException("double is out of range for int"));
}
return arg.longValue();
})),
STRING_TO_INT64(
(celOptions, runtimeEquality) ->
CelFunctionBinding.from(
Expand Down
35 changes: 35 additions & 0 deletions runtime/src/test/java/dev/cel/runtime/CelRuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ public void evaluate_anyPackedEqualityUsingProtoDifferencer_success() throws Exc
assertThat(evaluatedResult).isEqualTo(true);
}

@Test
public void evaluate_intOfMinIntDouble_returnsMinInt() throws Exception {
Cel cel = runtimeFlavor.builder().build();

Object result = cel.createProgram(cel.compile("int(-9223372036854775808.0)").getAst()).eval();

assertThat(result).isEqualTo(Long.MIN_VALUE);
}

@Test
public void evaluate_intOfBelowMinIntDouble_throws() throws Exception {
Cel cel = runtimeFlavor.builder().build();
CelRuntime.Program program =
cel.createProgram(cel.compile("int(-9223372036854777856.0)").getAst());

CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.NUMERIC_OVERFLOW);
assertThat(e).hasMessageThat().contains("double is out of range for int");
}

@Test
public void evaluate_intOfMinIntDouble_exactRangeDisabled_throws() throws Exception {
Cel cel =
runtimeFlavor
.builder()
.setOptions(CelOptions.current().enableExactDoubleToIntRange(false).build())
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("int(-9223372036854775808.0)").getAst());

CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.NUMERIC_OVERFLOW);
assertThat(e).hasMessageThat().contains("double is out of range for int");
}

@Test
public void evaluate_v1alpha1CheckedExpr() throws Exception {
// Note: v1alpha1 proto support exists only to help migrate existing consumers.
Expand Down
Loading