https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110244

            Bug ID: 110244
           Summary: False error of vector memory leak
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gimira8592 at aramask dot com
  Target Milestone: ---

Created attachment 55320
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55320&action=edit
the preprocessed file (*.i*) that triggers the bug, generated by adding
-save-temps

Version of GCC: (Ubuntu 12.2.0-17ubuntu1) 12.2.0
OS and Version: Ubuntu 23.04, Linux 6.2.0-20-generic
GCC configuration: -fanalyzer std=c++20

To trigger bug run this code

int main()
{
    std::vector<int> vec(2);
}

and when you try to compile it, you should see this error

leak of ‘vec.std::vector<int>::<anonymous>.std::_Vector_base<int,
std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl_data::_M_start’ [CWE-401] [-Werror=analyzer-malloc-leak]

Looks like a bug in GCC's static analyzer, because that code certainly does not
leak. Interestingly it only happens with C++20

It still happens with the latest GCC versions: https://godbolt.org/z/o6GbGcMaP 

Apparently its trigger by the addition of constexpr to the ctor/dtor in C++20
(and beyond). 

Apparently this error does not seem to occur with std::string but it do occur
with data type like int, double, long and char and fails to compile.

If you just create an uninitialized vector or default initialize with brace, it
will compile without any error but if you set it size or initialize with a
value then it will give memory leak error.

To show that this is related to constexpr on the ctor/dtor, you can #define
_GLIBCXX20_CONSTEXPR before #including <vector> in the C++20 version.

If you do that to effectively "define away" the constexpr in the C++20 version,
the static analyzer wont fail. Of course that is very much UB and pretty
dangerous (as you are actively meddling with the internals of the library and
the compilers assumptions), but it shows the point.
  • [Bug c++/110244] New: False err... gimira8592 at aramask dot com via Gcc-bugs

Reply via email to