C and C++


LANG.STRUCT.DNVD : delete with Non-Virtual Destructor

要旨

An object of derived polymorphic class type C is deleted through a pointer to its base class type B, but B has a non-virtual destructor.

プロパティ

クラス名 delete with Non-Virtual Destructor
日本語クラス名 delete with Non-Virtual Destructor
クラス分類 信頼性 (reliability)
ニーモニック LANG.STRUCT.DNVD
カテゴリー
AUTOSARC++14 AUTOSARC++14:A12-4-1 Destructor of a base class shall be public virtual, public override or protected non-virtual.
CWE CWE:1079 Parent Class without Virtual Destructor Method
  CWE:1087 Class with Virtual Method without a Virtual Destructor
CERT-CPP CERT-CPP:OOP52-CPP Do not delete a polymorphic object without a virtual destructor
JSF++ JSF++:78 All base classes with a virtual function shall define a virtual destructor.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="delete with Non-Virtual Destructor"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

namespace lang_struct_dnvd {

  class BaseNoExplDtor {
    virtual void f();
  public:
    BaseNoExplDtor(){};
  };

  class BaseExplDtor {
    virtual void f();
  public:
    BaseExplDtor(){};
    ~BaseExplDtor(){};
  };

  class BaseExplVirtualDtor {
    virtual void f();
  public:
    BaseExplVirtualDtor(){};
    virtual ~BaseExplVirtualDtor(){};
  };

  class BaseNotPolymorphic {
    void f();
  public:
    BaseNotPolymorphic();
    ~BaseNotPolymorphic();
  };

  class DerivedNoExplDtor : public BaseNoExplDtor { };
  class DerivedExplDtor : public BaseExplDtor { };
  class DerivedExplVirtualDtor : public BaseExplVirtualDtor { };
  class DerivedNotPolymorphic : public BaseNotPolymorphic { };

  class MultiDerived : public BaseNoExplDtor,
                       public BaseExplDtor,
                       public BaseExplVirtualDtor {};

  void useDerivedClasses(){
    BaseNoExplDtor *noexpldtor = new DerivedNoExplDtor();
    BaseExplDtor *expldtor = new DerivedExplDtor();
    BaseExplVirtualDtor *vdtor = new DerivedExplVirtualDtor();
    BaseNotPolymorphic *notpolymorphic = new DerivedNotPolymorphic();

    BaseNoExplDtor *noexpldtor_multi = new MultiDerived();
    BaseExplDtor *expldtor_multi = new MultiDerived();
    BaseExplVirtualDtor *vdtor_multi = new MultiDerived();

    delete noexpldtor;        // 'delete with Non-Virtual Destructor' warning issued here
    delete expldtor;          // 'delete with Non-Virtual Destructor' warning issued here
    delete vdtor;                                    // ok: BaseExplVirtualDtor declares a virtual destructor
    delete notpolymorphic;                           // ok: BaseNotPolymorphic and DerivedNotPolymorphic have no virtual functions so are not polymorphic
    delete noexpldtor_multi;  // 'delete with Non-Virtual Destructor' warning issued here
    delete expldtor_multi;    // 'delete with Non-Virtual Destructor' warning issued here
                                  // ('Misaligned Object' warning also issued)
    delete vdtor_multi;
  }
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。