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

Backslashes aren't properly handled: they seem to be escaped twice #624

Closed
MV-GH opened this issue Aug 12, 2022 · 3 comments
Closed

Backslashes aren't properly handled: they seem to be escaped twice #624

MV-GH opened this issue Aug 12, 2022 · 3 comments
Assignees

Comments

@MV-GH
Copy link

MV-GH commented Aug 12, 2022

1. Description

Backslashes aren't properly handled, It seems to escape them twice: \\\\ => \\ =>\

2. Exception

If you are seeing an exception, include the full exception details (message and stack trace).

Exception message:"parsing \"te\\st\" - Unrecognized escape sequence \\s."
Stack trace:
"   at System.Linq.Dynamic.Core.Parser.StringParser.ParseString(String s)\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseStringLiteral()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimaryStart()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimary()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseUnary()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMultiplicative()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAdditive()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseShiftOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()\r\n   at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor)\r\n   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(Type delegateType, ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)\r\n   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, Type itType, Type resultType, String expression, Object[] values)\r\n   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where(IQueryable source, ParsingConfig config, String predicate, Object[] args)\r\n   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where[TSource](IQueryable`1 source, ParsingConfig config, String predicate, Object[] args)\r\n   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where[TSource](IQueryable`1 source, String predicate, Object[] args)\r\n   at dynamicLinqTest.Program.Main(String[] args) in C:\\Users\\mave\\source\\repos\\dynamicLinqTest\\dynamicLinqTest\\Program.cs:line 17"

3. Fiddle or Project

FIDDLE:
https://dotnetfiddle.net/CVPBh3

REPRO

 var t1 = new List<string>() { "test" }.AsQueryable().Where("it == \"test\"").ToList(); // works
 var t2 = new List<string>(){ "t\\est" }.AsQueryable().Where("it == \"t\\est\"").ToList(); // Doesn't error but doesn't work
 var t3 = new List<string>() { "t\\est" }.AsQueryable().Where("it == \"t\\\\est\"").ToList(); // Works but it shouldn't
 var t4 = new List<string>() { "te\\st" }.AsQueryable().Where("it == \"te\\st\"").ToList(); // Throws an error, System.Linq.Dynamic.Core.Exceptions.ParseException: 'parsing "te\st" - Unrecognized escape sequence \s.'
 var t5 = new List<string>() { "test\\" }.AsQueryable().Where("it == \"test\\\"").ToList(); // Throws an error, System.Linq.Dynamic.Core.Exceptions.ParseException: 'Unterminated string literal'
@StefH StefH self-assigned this Aug 12, 2022
@StefH
Copy link
Collaborator

StefH commented Aug 12, 2022

@MV-GH
Because the backslash is used within an already escaped string : "it == \"XXX\", you need to double escape the backslash.
So t1 and t3 are valid scenarios.


Note that this behavior was not entirely correct in previous versions been fixed in version v1.2.13 ; issue #545.


See also
https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/e71b3560820d466237247334ffa4220af41b797c/test/System.Linq.Dynamic.Core.Tests/Parser/StringParserTests.cs

@MV-GH
Copy link
Author

MV-GH commented Aug 12, 2022

Thanks for the quick response! Could this behavior be added to the documentation?

@StefH
Copy link
Collaborator

StefH commented Aug 17, 2022

Docs are updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants