Suppress clang-tidy warnings about freed memory in wxCharBuffer

These warnings seem bogus as clang-tidy provides the following
explanation for them:

include/wx/buffer.h:156:44: warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete]
    const CharType *data() const { return  m_data->Get(); }
                                           ^
include/wx/buffer.h:127:9: note: Calling 'wxScopedCharTypeBuffer::DecRef'
        DecRef();
        ^~~~~~~~
include/wx/buffer.h:194:14: note: Assuming the condition is false
        if ( m_data == GetNullData() ) // exception, not ref-counted
             ^~~~~~~~~~~~~~~~~~~~~~~
include/wx/buffer.h:194:9: note: Taking false branch
        if ( m_data == GetNullData() ) // exception, not ref-counted
        ^
include/wx/buffer.h:196:14: note: Assuming the condition is true
        if ( --m_data->m_ref == 0 )
             ^~~~~~~~~~~~~~~~~~~~
include/wx/buffer.h:196:9: note: Taking true branch
        if ( --m_data->m_ref == 0 )
        ^
include/wx/buffer.h:197:13: note: Memory is released
            delete m_data;
            ^~~~~~~~~~~~~
include/wx/buffer.h:127:9: note: Returning; memory was released
        DecRef();
        ^~~~~~~~

which assumes that the reference count became 0, and thus data was
freed, while it's still used, which means that the reference count is
non-zero.

Suppress the warnings to allow applications using clang-tidy to avoid
this warning whenever they use wxCharBuffer (possibly implicitly).
This commit is contained in:
Vadim Zeitlin
2024-08-10 20:07:56 +02:00
parent a15ebd6b42
commit 0aa8a39e2b
+10
View File
@@ -211,7 +211,17 @@ public:
DecRef();
}
// For some reasong clang-tidy gives a warning about using freed memory
// here even when this is not at all the case, seemingly because it doesn't
// follow reference counting logic, i.e. it assumes that it's possible to
// delete the data even when it's still referenced.
//
// Suppress the warning as it's extremely annoying to get it for every use
// of wxCharBuffer.
//
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)
CharType *data() { return m_data->Get(); }
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)
const CharType *data() const { return m_data->Get(); }
operator const CharType *() const { return data(); }
CharType operator[](size_t n) const { return data()[n]; }