From 07fff187e3c6c54a48ea79fb6b91b836932bc311 Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Sat, 21 Nov 2026 22:19:21 +0100 Subject: [PATCH] fix: throw when a closure-expecting op is not passed a closure Before it just did not push anything on the stack, which would defer the error --- .../biscuit/datalog/expressions/Op.java | 19 ++++---- .../biscuit/datalog/ExpressionTest.java | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index 46f8529d..0180c4cc 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -541,13 +541,13 @@ public void evaluate( if (left instanceof Term.Bool && right instanceof Closure) { if (((Term.Bool) left).value()) { Closure closure = (Closure) right; - Term result = closure.call(variables, temporarySymbolTable, steps); - if (result instanceof Term.Bool) { - stack.push(result); - } + stack.push(closure.call(variables, temporarySymbolTable, steps)); } else { stack.push(new Term.Bool(false)); } + } else { + throw new Error.Execution( + Error.Execution.Kind.InvalidType, "invalid operands for lazy and op"); } break; case LazyOr: @@ -556,11 +556,11 @@ public void evaluate( stack.push(new Term.Bool(true)); } else { Closure closure = (Closure) right; - Term result = closure.call(variables, temporarySymbolTable, steps); - if (result instanceof Term.Bool) { - stack.push(result); - } + stack.push(closure.call(variables, temporarySymbolTable, steps)); } + } else { + throw new Error.Execution( + Error.Execution.Kind.InvalidType, "invalid operands for lazy or op"); } break; case Intersection: @@ -735,6 +735,9 @@ public void evaluate( if (e instanceof Error.TooManySteps) throw e; stack.push(right); } + } else { + throw new Error.Execution( + Error.Execution.Kind.InvalidType, "left operand of try_or op is not a closure"); } break; default: diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java index 44e34a5f..e2a9eac7 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java @@ -6,6 +6,7 @@ package org.eclipse.biscuit.datalog; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.Arrays; @@ -148,4 +149,50 @@ public void testIntersectionAndContains() throws Error { new TemporarySymbolTable(symbolTable), new StepCounter(new RunLimits().getMaxSteps()))); } + + private static Term evaluate(Op... ops) throws Error { + return new Expression(new ArrayList<>(Arrays.asList(ops))) + .evaluate( + new HashMap<>(), + new TemporarySymbolTable(new SymbolTable()), + new StepCounter(new RunLimits().getMaxSteps())); + } + + private static Op.Closure closure(Op... ops) { + return new Op.Closure(new ArrayList<>(), new ArrayList<>(Arrays.asList(ops))); + } + + @Test + public void lazyOpsReturnTheClosureResult() throws Error { + assertEquals( + new Term.Integer(1), + evaluate( + new Term.Bool(true), closure(new Term.Integer(1)), new Op.Binary(Op.BinaryOp.LazyAnd))); + assertEquals( + new Term.Integer(1), + evaluate( + new Term.Bool(false), closure(new Term.Integer(1)), new Op.Binary(Op.BinaryOp.LazyOr))); + } + + @Test + public void lazyOpsRejectInvalidOperands() { + assertThrows( + Error.Execution.class, + () -> + evaluate(new Term.Bool(true), new Term.Bool(true), new Op.Binary(Op.BinaryOp.LazyAnd))); + assertThrows( + Error.Execution.class, + () -> + evaluate( + new Term.Integer(1), + closure(new Term.Bool(true)), + new Op.Binary(Op.BinaryOp.LazyOr))); + } + + @Test + public void tryOrRejectsANonClosureOperand() { + assertThrows( + Error.Execution.class, + () -> evaluate(new Term.Integer(1), new Term.Integer(2), new Op.Binary(Op.BinaryOp.TryOr))); + } }