Skip to content

Commit

Permalink
Merge pull request #557 from BenJBailey/master
Browse files Browse the repository at this point in the history
Fix: If statement comparing string and null value results in object reference error
  • Loading branch information
xoofx authored Jul 13, 2024
2 parents 38df149 + 8a2c229 commit 3a695be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ null != null -> false
null == 1 -> false
1 != null -> true
null != 1 -> true
"a" == null -> false
null == "b" -> false
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ null != null -> {{ null != null }}
null == 1 -> {{ null == 1 }}
1 != null -> {{ 1 != null }}
null != 1 -> {{ null != 1 }}
"a" == null -> {{ "a" == null }}
null == "b" -> {{ null == "b" }}
8 changes: 6 additions & 2 deletions src/Scriban/Syntax/Expressions/ScriptBinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ public static object Evaluate(TemplateContext context, SourceSpan span, ScriptBi
case ScriptBinaryOperator.CloseInterpolated:
try
{
if (leftValue is string || rightValue is string || leftValue is char || rightValue is char)
if (leftValue == null || rightValue == null)
{
return CalculateOthers(context, span, op, leftSpan, leftValue, rightSpan, rightValue);
}
else if (leftValue is string || rightValue is string || leftValue is char || rightValue is char)
{
if (leftValue is char leftChar) leftValue = leftChar.ToString(context.CurrentCulture);
if (rightValue is char rightChar) rightValue = rightChar.ToString(CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -484,7 +488,7 @@ private static object CalculateOthers(TemplateContext context, SourceSpan span,
switch (op)
{
case ScriptBinaryOperator.CompareEqual:
return false;
return false;
case ScriptBinaryOperator.CompareNotEqual:
return true;

Expand Down

0 comments on commit 3a695be

Please sign in to comment.