jenkinsci / groovy-sandbox

(Deprecated) Compile-time transformer to run Groovy code in a restrictive sandbox
MIT License
123 stars 60 forks source link

Logical Operators #10

Closed shrtminded closed 10 years ago

shrtminded commented 10 years ago

I am receiving a IllegalArgumentException when I have an logical operation.
For example:

if ((1==1) && (2==2)) {
  // do something
}

Cause this exception to be thrown

java.lang.IllegalArgumentException: 164
    at org.kohsuke.groovy.sandbox.impl.Ops.binaryOperatorMethods(Ops.java:29)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedBinaryOp(Checker.java:311)
    at org.kohsuke.groovy.sandbox.impl.Checker$checkedBinaryOp$2.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:173)

I am not sure if this is related to issue #9.

shrtminded commented 10 years ago

I was able to fix this issue locally but not sure if this is how you would want to fix it. Below is the patch for the changes I made if you want to use them ...

From 897f595008f18798503e6a339c55c91c4455bb9d Mon Sep 17 00:00:00 2001
Date: Wed, 15 Jan 2014 08:40:21 -0500
Subject: [PATCH] Fixing logical and/or statements

---
 src/main/java/org/kohsuke/groovy/sandbox/impl/Ops.java | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/main/java/org/kohsuke/groovy/sandbox/impl/Ops.java b/src/main/java/org/kohsuke/groovy/sandbox/impl/Ops.java
index 57b3f02..e8fcc62 100644
--- a/src/main/java/org/kohsuke/groovy/sandbox/impl/Ops.java
+++ b/src/main/java/org/kohsuke/groovy/sandbox/impl/Ops.java
@@ -64,6 +64,8 @@ public class Ops {
         b.put(BITWISE_OR,"or");
         b.put(BITWISE_AND,"and");
         b.put(BITWISE_XOR,"xor");
+        b.put(LOGICAL_OR,"or");
+        b.put(LOGICAL_AND,"and");
         b.put(LEFT_SHIFT,"leftShift");
         b.put(RIGHT_SHIFT,"rightShift");
         b.put(RIGHT_SHIFT_UNSIGNED,"rightShiftUnsigned");
-- 
1.8.3.4 (Apple Git-47)
kohsuke commented 10 years ago

I've fixed this issue. The patch is actually incorrect because the "or" method is for overloading bitwise-or, which is not the same thing as logical or.