google / google/leveldb

Memory Leak in leveldb_filterpolicy_create_bloom Function

Open
#1,182 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.4k
Forks
8.2k
PR merge metrics
No merged PRs in 30d

Description

### Title
Memory Leak in leveldb_filterpolicy_create_bloom Function

### Body
**Description**
There appears to be a potential memory leak in the implementation of `leveldb_filterpolicy_create_bloom` in `db/c.cc`. The leak occurs when an instance of `Wrapper`, which internally manages a `FilterPolicy` object, is not properly destroyed under certain conditions, leading to memory not being freed.

**Leak Details**
- The function creates a `Wrapper` object, which owns a `FilterPolicy` instance allocated by `NewBloomFilterPolicy`.
- The `Wrapper` destructor is supposed to manage the lifecycle of the `FilterPolicy` instance. However, if an exception or an early return occurs before the `Wrapper` object is assigned to a managing pointer or if the `leveldb_filterpolicy_destroy` function does not perform the deletion correctly, the allocated memory is not freed, resulting in a memory leak.

**Steps to Reproduce**
1. Call `leveldb_filterpolicy_create_bloom` with a specific `bits_per_key`.
2. Simulate an exception or an early return before the function completes.
3. Observe that the memory allocated for the `FilterPolicy` instance is not freed.

**Expected Behavior**
All allocated memory should be properly managed and freed when no longer needed.

**Possible Fix**
Ensure that all error paths and exceptional conditions are handled appropriately, and that the `leveldb_filterpolicy_destroy` function correctly deletes the `Wrapper` object.

**System Information**
- Operating System: [Your Operating System]
- Architecture: [e.g., x86_64]
- LevelDB Version: [Specify the version or commit hash]

### Additional Information
Please consider reviewing the exception safety of the function to ensure that resources are always released correctly, regardless of the execution path.

### Labels
bug, memory-leak

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.