fix: replace manual destructor calls in constructors with explicit cleanup - #114
Open
JoeSergen wants to merge 2 commits into
Open
fix: replace manual destructor calls in constructors with explicit cleanup#114JoeSergen wants to merge 2 commits into
JoeSergen wants to merge 2 commits into
Conversation
…ath argument When fs_watch() is called with a non-null path, the else branch incorrectly calls trace_file_init() instead of mountsnoop_init(). This is a copy-paste error from file_watch(). The correct behavior is to initialize mountsnoop for filesystem event monitoring. Signed-off-by: JoeSergen <jxq142857@163.com>
…eanup Calling this->~ClassName() from constructor catch blocks is fragile and violates C++ best practices. In RingBuffer's Normal constructor, type was set after the first try block, causing the destructor to run the BPF cleanup path on uninitialized data. Changes: - ring-buffer.cpp: replace this->~RingBuffer() with explicit munmap/close in BPF constructor; move type=RING_BUF_TYPE_NORMAL before try block and replace destructor calls with SAFE_DELETE in Normal constructor - bpf-manager.cpp: replace this->~BPFManager() with SAFE_DELETE - data-map.cpp: replace this->~DataMap() with SAFE_DELETE in reverse init order Signed-off-by: JoeSergen <jxq142857@163.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
this->~ClassName()calls in constructor catch blocks with explicit member cleanup. 5 call sites across 3 files.Root Cause
The Pattern (all 3 constructors)
Calling a destructor from a constructor is fragile: if the destructor is later modified to use members that haven't been initialized yet, undefined behavior follows.
The Concrete Crash Bug (RingBuffer Normal constructor)
In the Normal constructor,
type = RING_BUF_TYPE_NORMALwas set after the first try block. The destructor dispatches ontype:Before this fix:
If
new SharedMemory()succeeds butnew SpinLock()fails,comsumer_indexpoints to a valid shared-memory address.(ulong)comsumer_index > 0evaluates to TRUE, andmunmap()is called on a kernel pointer → SIGSEGV.Changes
1. ring-buffer.cpp — BPF constructor
err_outlabelReplaced
this->~RingBuffer()with explicit cleanup using properMAP_FAILEDchecks (instead of fragile(ulong)ptr > 0):err_out: if (producer_index && producer_index != MAP_FAILED) { munmap(...); } if (comsumer_index && comsumer_index != MAP_FAILED) { munmap(...); } if (epoll_fd >= 0) { close(epoll_fd); } throw exc;2. ring-buffer.cpp — Normal constructor
type = RING_BUF_TYPE_NORMAL;before the first try block (fixes the crash)SAFE_DELETEon heap-allocated members only3. bpf-manager.cpp
4. data-map.cpp
Non-heap Members
These members are pointer assignments into shared memory — they point to fields inside heap-allocated objects, not independently allocated memory. They are NOT freed by cleanup:
bpf_ref_cnt→&m_shm->bpf_ref_cntcomsumer_index→&shm_ctl->rdiproducer_index→&shm_ctl->wrim_entrys→m_rb->buf()m_idx→&m_shm->data_map_idxVerification
SAFE_DELETEis the project's established pattern (include/com.h:432)Closes #113