Skip to content
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

[BUG] Expression IIF(X, IfTrue, IfFalse) evaluates both IfTrue and IfFalse #1458

Closed
nightroman opened this issue Feb 9, 2020 · 1 comment

Comments

@nightroman
Copy link
Contributor

Version

LiteDB 5.0.1 and the latest from repo, Windows 10, net45

Describe the bug

In the expression IIF(X, IfTrue, IfFalse), both IfTrue and IfFalse seem to be evaluated.

As a result, evaluation of one of them may fail when X is supposed to avoid this evaluation with invalid arguments.

Code to Reproduce

var ex1 = BsonExpression.Create("IIF(LENGTH($.x) >= 5, SUBSTRING($.x, 0, 5), \"too-short\")");
var doc1 = new BsonDocument();

// OK ("12345")
doc1["x"] = "123456789";
var r1 = ex1.ExecuteScalar(doc1);
Console.WriteLine($"{r1}");

// KO (expected: "too-short", actual: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.)
doc1["x"] = "123";
var r2 = ex1.ExecuteScalar(doc1);
Console.WriteLine($"{r2}");

Expected behavior

See code comments.

Screenshots/Stacktrace

Output of the above code:

"12345"

Unhandled Exception: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
   at System.String.Substring(Int32 startIndex, Int32 length)
   at LiteDB.BsonExpressionMethods.SUBSTRING(BsonValue value, BsonValue startIndex, BsonValue length)
   at lambda_method(Closure , IEnumerable`1 , BsonDocument , BsonValue , Collation , BsonDocument )
   at LiteDB.BsonExpression.ExecuteScalar(IEnumerable`1 source, BsonDocument root, BsonValue current, Collation collation)
   at LiteDB.BsonExpression.ExecuteScalar(BsonDocument root, Collation collation)
@mbdavid
Copy link
Collaborator

mbdavid commented Feb 9, 2020

Hi @nightroman, same problem in AND/OR logical test. Upgrade to use Expression.Conditional linq expression. Thanks!

@mbdavid mbdavid added the fixed label Feb 9, 2020
@mbdavid mbdavid closed this as completed Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants