Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG] Expression X AND Y should not evaluate Y when X is false #1452

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

[BUG] Expression X AND Y should not evaluate Y when X is false #1452

nightroman opened this issue Feb 8, 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 X AND Y, Y is evaluated even if X is evaluated to false.

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

Code to Reproduce

var ex1 = BsonExpression.Create("LENGTH($.x) >= 5 AND SUBSTRING($.x, 0, 5) = \"12345\"");
var doc1 = new BsonDocument();

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

// KO (expected: false, actual: exception)
doc1["x"] = "123";
var r2 = ex1.ExecuteScalar(doc1);
Console.WriteLine($"{r2}");

Expected behavior

See code comments.

Screenshots/Stacktrace

Output of the above code:

true

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)
@nightroman nightroman added the bug label Feb 8, 2020
@mbdavid mbdavid closed this as completed in 35e4935 Feb 8, 2020
@mbdavid
Copy link
Collaborator

mbdavid commented Feb 8, 2020

Hi @nightroman, thanks again for discover this. I change expression implementation to use AndAlso and OrElse in BsonExpression parser and compiler. Will be avaiable in next version.

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

No branches or pull requests

2 participants