Skip to content

Commit 9f2bf0c

Browse files
committed
CSHARP-5139: Coverity analysis defect 127428: Explicit null dereferenced.
1 parent 5c54752 commit 9f2bf0c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/WindowMethodToAggregationExpressionTranslator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
292292

293293
if (method.IsOneOf(__unaryMethods))
294294
{
295+
ThrowIfSelectorTranslationIsNull(selectorTranslation);
295296
var @operator = GetUnaryWindowOperator(method);
296297
var ast = AstExpression.UnaryWindowExpression(@operator, selectorTranslation.Ast, window);
297298
var serializer = BsonSerializer.LookupSerializer(method.ReturnType); // TODO: use correct serializer
@@ -313,6 +314,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
313314

314315
if (method.IsOneOf(__derivativeOrIntegralMethods))
315316
{
317+
ThrowIfSelectorTranslationIsNull(selectorTranslation);
316318
WindowTimeUnit? unit = default;
317319
if (HasArgument<Expression>(parameters, "unit", arguments, out var unitExpression))
318320
{
@@ -327,6 +329,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
327329

328330
if (method.IsOneOf(__exponentialMovingAverageMethods))
329331
{
332+
ThrowIfSelectorTranslationIsNull(selectorTranslation);
330333
var weightingExpression = arguments[2];
331334
var weighting = weightingExpression.GetConstantValue<ExponentialMovingAverageWeighting>(expression);
332335

@@ -337,6 +340,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
337340

338341
if (method.IsOneOf(__shiftMethods))
339342
{
343+
ThrowIfSelectorTranslationIsNull(selectorTranslation);
340344
var byExpression = arguments[2];
341345
var by = byExpression.GetConstantValue<int>(expression);
342346

@@ -435,6 +439,14 @@ private static bool HasArgument<TExpression>(ParameterInfo[] parameters, string
435439
return false;
436440
}
437441

442+
private static void ThrowIfSelectorTranslationIsNull(AggregationExpression selectTranslation)
443+
{
444+
if (selectTranslation == null)
445+
{
446+
throw new Exception("selectorTranslation is unexpectedly null.");
447+
}
448+
}
449+
438450
private static AggregationExpression TranslateSelector(TranslationContext context, LambdaExpression selectorLambda, IBsonSerializer inputSerializer)
439451
{
440452
var selectorParameter = selectorLambda.Parameters[0];

0 commit comments

Comments
 (0)