boostorg / boostorg/multiprecision

[clang-tidy] argument to operator delete is the address of a local stack variable, which is not memory allocated by 'new'

Open
#359 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
265
Forks
128
Avg merge
4h 48m
Merged PRs (30d)
2

Description

`clang-tidy` on a project brought this to my attention:
```txt
/usr/lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/ext/new_allocator.h:125:2: error: Argument to operator delete is the address of a local stack variable, which is not memory allocated by 'new' [clang-analyzer-cplusplus.NewDelete,-warnings-as-errors]
::operator delete(__p);
^
...
my_private_include_path/include/boost/multiprecision/number.hpp:2005:25: note: Calling 'number::str'
std::string s = r.str(d, os.flags());
^
my_private_include_path/include/boost/multiprecision/number.hpp:808:14: note: Calling 'cpp_int_backend::str'
return m_backend.str(digits, f);
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1920:14: note: Calling 'cpp_int_backend::do_get_string'
return do_get_string(f, trivial_tag());
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1822:11: note: Assuming the condition is false
if ((f & std::ios_base::oct) == std::ios_base::oct)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1822:7: note: Taking false branch
if ((f & std::ios_base::oct) == std::ios_base::oct)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1824:16: note: Assuming the condition is false
else if ((f & std::ios_base::hex) == std::ios_base::hex)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1824:12: note: Taking false branch
else if ((f & std::ios_base::hex) == std::ios_base::hex)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1830:11: note: 'base' is not equal to 8
if (base == 8 || base == 16)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1830:11: note: Left side of '||' is false
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1830:24: note: 'base' is not equal to 16
if (base == 8 || base == 16)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1830:7: note: Taking false branch
if (base == 8 || base == 16)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1876:10: note: Taking false branch
if (t.sign())
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1881:14: note: Assuming the condition is false
if (this->size() == 1)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1881:10: note: Taking false branch
if (this->size() == 1)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1889:13: note: Loop condition is false. Execution continues on line 364
while (eval_get_sign(t) != 0)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1904:10: note: Calling implicit destructor for 'cpp_int_backend<0, 0, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, std::allocator>'
}
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:1904:10: note: Calling '~cpp_int_base'
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:364:11: note: Assuming field 'm_internal' is false
if (!m_internal)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:364:7: note: Taking true branch
if (!m_internal)
^
my_private_include_path/include/boost/multiprecision/cpp_int.hpp:365:10: note: Calling 'new_allocator::deallocate'
allocator().deallocate(limbs(), capacity());
^
/usr/lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/ext/new_allocator.h:125:2: note: Argument to operator delete is the address of a local stack variable, which is not memory allocated by 'new'
::operator delete(__p);
^
```
I am not sure but it seems that `clang-tidy` misinterprets the call to the custom allocator `scoped_shared_storage`.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with boost/multiprecision/number.hpp around line 2005 and boost/multiprecision/cpp_int.hpp around lines 1822-1904, then inspect the allocator path at line 365 and the scoped_shared_storage implementation. Reproduce the clang-tidy diagnostic with the reported string conversion, determine whether the warning is valid, and document or test the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.