-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WW-5118 Fix OgnlOps equal #116
Conversation
Looks good, @aleksandr-m could you link the issue from SO? |
Thank you for the heads-up, @lukaszlenart ! This change may break the following test involving a custom number implementation. public void testCustomNumberEquality() throws Exception {
Assert.assertTrue(OgnlOps.equal(new MyNumber(1.234), new MyNumber(1.234)));
}
class MyNumber extends Number {
private Double d;
public MyNumber(Double d) {
super();
this.d = d;
}
@Override
public int intValue() {
return d.intValue();
}
@Override
public long longValue() {
return d.longValue();
}
@Override
public float floatValue() {
return d.floatValue();
}
@Override
public double doubleValue() {
return d.doubleValue();
}
} |
@harawata You need to override |
@aleksandr-m , |
Hi. Thanks to @aleksandr-m for advising of the issue and proposing a potential fix PR. 😄 Also, thanks to @lukaszlenart for the heads-up for the discussion. One concern might be that, according to the repository history, The proposed change: https://github.com/jkuhnert/ognl/blob/c496b654c4683829b397b4a3c6d8de5db724c42b/src/main/java/ognl/OgnlOps.java#L811-L814 appears to remove the "fall-back" logic that attempts to compare the two parameters (if The original logic already called I guess the question is why the original "fall-back" logic was present for If someone knows the history of why Removing the "fall-back" check might fix the reported issue, but it might also change the behaviour unexpectedly for some other existing OGNL usage. Maybe the original One possible alternative implementation that passes @aleksandr-m 's new unit tests could be something like:
There may be logic flaws in the above alternative (it does not address the scenario @harawata identified for instance), but something like it might be less risky than completely removing the old logic. Maybe a smaller incremental change that keeps some of the "fuzzy numeric logic" would be safer ? |
This check doesn't work if result is
And what about comparing different objects? Currently
If someone wants to compare objects by properties the |
I think public void testLongVsString() throws Exception {
final Long v1 = 100L;
final String v2 = "100";
assertTrue(OgnlOps.equal(v1, v2));
} And the fall-back logic made it possible to compare two different Not that I recommend it, a trivial patch like the following might be safer if you're worried about backward compatibility. diff --git a/src/main/java/ognl/OgnlOps.java b/src/main/java/ognl/OgnlOps.java
index 7e990df..cbadafa 100644
--- a/src/main/java/ognl/OgnlOps.java
+++ b/src/main/java/ognl/OgnlOps.java
@@ -813,7 +813,8 @@ public abstract class OgnlOps implements NumericTypes
if (v1 == null) return v2 == null;
if (v1 == v2 || isEqual(v1, v2)) return true;
if (v1 instanceof Number && v2 instanceof Number)
- return ((Number) v1).doubleValue() == ((Number) v2).doubleValue();
+ return ((Number) v1).longValue() == ((Number) v2).longValue()
+ && ((Number) v1).doubleValue() == ((Number) v2).doubleValue();
return false;
}
|
Hi. There are fair points raised by both @aleksandr-m and @harawata . The old "fall-back" logic for A larger concern would probably be preserving the additional checks in
If someone needs to do specialized |
I built a SNAPSHOT version of OGNL based on this PR and I have ran all the Struts tests based on that - everything looks good. So I assume the change is fine, but I think @JCgH4164838Gh792C124B5 has raised a valid concern that this code was here for performance reason. I would use @harawata or @JCgH4164838Gh792C124B5 latest proposal, @aleksandr-m wdyt? |
Used @JCgH4164838Gh792C124B5 version. Thanks. |
Nice, if no more objections I can prepare a release. @aleksandr-m do you want to port this change to OGNL 3.1.x to be used with Struts 2.5 or maybe it is not needed? |
@lukaszlenart Done. |
3.2.20 released |
Originally, found on StackOverflow - https://stackoverflow.com/questions/66138007/struts-ognl-long-overflow