-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory Leak in Status Class and Usage in leveldb #1184
Comments
I do not see how this is an issue. The static member function
Looking at the implementation of the copy constructor, Looking at the implementation of the copy assignment operator. The target object's
This is plainly incorrect. The destructor of
The code example you show leaks memory itself, by allocating a new Also, from your code, this looks strange:
Why would you treat |
Running this code causes boost unit testing framework to report memory leak:
Run output: Compiler & Platform:
|
Description
There appears to be a potential memory leak issue within the Status class of the leveldb project. This issue was discovered during fuzz testing and further analysis of the source code. The memory leak is primarily associated with the dynamic memory allocation and management within the Status class' constructors and CopyState method.
Reproduce
Memory leaks were detected using a fuzzing tool, which revealed issues during operations involving error status creation and management.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t data, size_t size) {
leveldb::Slice slice1(reinterpret_cast<const char>(data), size);
leveldb::Slice slice2(reinterpret_cast<const char*>(data), size);
leveldb::Status status = leveldb::Status::NotFound(slice1, slice2);
leveldb::Iterator* iterator = leveldb::NewErrorIterator(status);
return 0;
}
Analysis
1.Status::CopyState Method:
This method allocates memory for a new state string but does not have a corresponding deallocation mechanism. If the returned pointer from CopyState is not properly managed by the calling function, it can lead to memory leaks.
2.Status Constructor:
The constructor for creating an error Status object dynamically allocates memory to hold the error message and associated data. However, there is no explicit destructor in the Status class to deallocate this memory once the Status object is no longer in use.
Potential Impact
If these memory allocations are not properly managed, it can lead to memory leaks, which may affect the performance and reliability of applications using the leveldb library, especially in long-running applications where repeated status errors might occur.
Suggested Fix
Implement a destructor in the Status class that properly deallocates the state_ member if it is not nullptr.
Ensure that any usage of CopyState handles the allocated memory correctly, preferably by using smart pointers or ensuring that the memory is deleted when no longer needed.
Additional Information
The memory leak is evident from both fuzz test results and direct code analysis. This issue could potentially be resolved by revising memory management practices within the Status class implementation.
The text was updated successfully, but these errors were encountered: