You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This checker reports cases where an array of polymorphic objects are
deleted as their base class. Deleting an array where the array's static
type is different from its dynamic type is undefined.
Since the checker is similar to DeleteWithNonVirtualDtorChecker, I
refactored that checker to support more detection types.
This checker corresponds to the SEI Cert rule EXP51-CPP: Do not delete
an array through a pointer of the incorrect type.
Differential Revision: https://reviews.llvm.org/D158156
Copy file name to clipboardExpand all lines: clang/docs/analyzer/checkers.rst
+31-3Lines changed: 31 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1834,6 +1834,30 @@ Either the comparison is useless or there is division by zero.
1834
1834
alpha.cplusplus
1835
1835
^^^^^^^^^^^^^^^
1836
1836
1837
+
.. _alpha-cplusplus-ArrayDelete:
1838
+
1839
+
alpha.cplusplus.ArrayDelete (C++)
1840
+
"""""""""""""""""""""""""""""""""
1841
+
Reports destructions of arrays of polymorphic objects that are destructed as their base class.
1842
+
This checker corresponds to the CERT rule `EXP51-CPP: Do not delete an array through a pointer of the incorrect type <https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP51-CPP.+Do+not+delete+an+array+through+a+pointer+of+the+incorrect+type>`_.
1843
+
1844
+
.. code-block:: cpp
1845
+
1846
+
class Base {
1847
+
virtual ~Base() {}
1848
+
};
1849
+
class Derived : public Base {}
1850
+
1851
+
Base *create() {
1852
+
Base *x = new Derived[10]; // note: Casting from 'Derived' to 'Base' here
1853
+
return x;
1854
+
}
1855
+
1856
+
void foo() {
1857
+
Base *x = create();
1858
+
delete[] x; // warn: Deleting an array of 'Derived' objects as their base class 'Base' is undefined
1859
+
}
1860
+
1837
1861
.. _alpha-cplusplus-DeleteWithNonVirtualDtor:
1838
1862
1839
1863
alpha.cplusplus.DeleteWithNonVirtualDtor (C++)
@@ -1842,13 +1866,17 @@ Reports destructions of polymorphic objects with a non-virtual destructor in the
1842
1866
1843
1867
.. code-block:: cpp
1844
1868
1869
+
class NonVirtual {};
1870
+
class NVDerived : public NonVirtual {};
1871
+
1845
1872
NonVirtual *create() {
1846
-
NonVirtual *x = new NVDerived(); // note: conversion from derived to base
1847
-
// happened here
1873
+
NonVirtual *x = new NVDerived(); // note: Casting from 'NVDerived' to
1874
+
// 'NonVirtual' here
1848
1875
return x;
1849
1876
}
1850
1877
1851
-
void sink(NonVirtual *x) {
1878
+
void foo() {
1879
+
NonVirtual *x = create();
1852
1880
delete x; // warn: destruction of a polymorphic object with no virtual
0 commit comments