Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I am interested to try it on the platforms:
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si vous n'avez toujours pas reçu de réponse, vérifiez votre dossier
Spam/Junk et cliquez sur le bouton "Not Spam".
De cette façon, vous ne manquerez la réponse de notre équipe.

>
>
>
V3153. Dereferencing the result of null…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V3153. Dereferencing the result of null-conditional access operator can lead to NullReferenceException.

26 Avr 2021

The analyzer found a potential error that may cause the null reference's dereferencing. The code contains the '?.' operator's result that is dereferenced immediately - explicitly or implicitly.

A 'NullReferenceException' exception may be thrown, for example, in cases below:

  • You used the null-conditional operator on a potentially null element, placed the expression in parentheses, and dereferenced the result.
  • The 'foreach' statement contains the null-conditional operator.

The scenarios above may lead to one of the following:

  • The program throws a 'NullReferenceException' exception if you conditionally access a 'null' reference.
  • The program always works correctly, because the reference you use for conditional access is never 'null'. In the second case, checking for 'null' is unnecessary.

Let's take a closer look at the first case. This code may throw an exception:

var t = (obj?.ToString()).GetHashCode();

In the 'obj?.ToString()' expression, if the 'obj' object is 'null', the 'ToString()' method is not called. This is how the conditional access operator works. However, since the 'GetHashCode' method is outside the null-conditional expression, it is called no matter the expression's result.

Below is the fixed code:

var t = obj?.ToString().GetHashCode();

The expression above does not have the dangerous dereferencing. Additionally, the 't' variable now has the 'Nullable<int>' type, which correctly reflects its contents as potentially containing the 'null' value.

Let's take a look at a different example. Here, checking for 'null' is excessive because of the safe dereferencing:

object obj = GetNotNullString();
....
var t = ((obj as String)?.Length).GetHashCode();

This code always works correctly. The 'obj' object is always of the 'String' type, therefore checking type after casting is unnecessary.

Below is the fixed code:

var t = ((String)obj).Length.GetHashCode();

The example below shows a foreach statement that contains the null-conditional operator:

void DoSomething(string[] args)
{
  foreach (var str in args?.Where(arg => arg != null))
    ....
}

If the 'args' parameter is 'null', the 'args?.Where(....)' expression also evaluates to 'null' because of the '?.' operator. When the 'foreach' loop to iterates through the collection, a 'NullReferenceException' exception is thrown. This happens because the 'GetEnumerator()' method is implicitly called for the 'args?.Where(....)', and this dereferences the null reference.

You can fix the code in the following way:

void DoSomething(string[] args)
{
  foreach (var str in    args?.Where(arg => arg != null)
                      ?? Enumerable.Empty<string>())
    ....
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3153 diagnostic.