Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V1114. Suspicious use of type conversio…
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++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V1114. Suspicious use of type conversion operator when working with COM interfaces. Consider using the 'QueryInterface' member function.

27 Sep 2024

The analyzer has detected a suspicious use of the type conversion operator when working with COM interfaces.

Such usage can lead to several issues, including:

  • The reference counter is not incremented.
  • Conversion to an incompatible type or interface may occur—for example, in cases involving multiple inheritance or changes to an interface version.
  • ABI compatibility issues.

The correct way to work with COM interfaces is to use the 'QueryInterface' function, which is specifically designed for this purpose. This is because COM is a programming language-independent technology, allowing COM objects to be implemented in languages other than C++ and to reside in address spaces separate from that of the calling process.

The 'QueryInterface' function should:

  • check that the requested interface is accessible;
  • return a pointer to the requested interface;
  • increment the object reference counter.

Here is a synthetic example of the error: the code includes COM interfaces, 'IDraw' and 'IShape', which handle a certain geometric shape:

interface IDraw : public IUnknown
{
  ....
  virtual HRESULT Draw() = 0;
  ....
};

interface IShape : public IUnknown
{
  ....
  virtual HRESULT GetArea(double *area) = 0;
  ....
};

There is also the 'Circle' COM object that implements the 'IDraw' and 'IShape' interfaces:

class Circle : public IDraw, public IShape
{
  ....
};

Look at an example of the incorrect COM object handling via the 'IDraw' interface:

void foo(IDraw *ptrIDraw)
{
  IShape *ptrIShape = dynamic_cast<IShape*>(ptrIDraw);
  ....
  if (ptrIShape) 
    ptrIShape->GetArea(area);
  ....
}

In the above example, the reference counter for the 'Circle' object is not incremented. To increment and decrement the counter, call the 'AddRef' and 'Release' functions respectively. Thus, using the 'QueryInterface' function is recommended, as it automatically handles the reference counting mechanism.

Here is the fixed code:

void foo(IDraw *ptrIDraw)
{
  IShape *ptrIShape = nullptr;
  ....
  if (SUCCEEDED(ptrIDraw->QueryInterface(IID_IShape, &ptrIShape))
  ....
}

This diagnostic is classified as:

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 want to join the test
* 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 l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam