Fix: Critical dangling pointer bug in NexusBridge::nxmFilesAvailable #2288
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.
The Problem
I discovered a critical dangling pointer bug in the
NexusBridge::nxmFilesAvailablefunction. The original code stored the address of a stack-allocated variable (temp) in a list. When the function returned, this created a dangling pointer, leading to a high risk of application crashes or memory corruption when thefilesAvailablesignal was processed.My Solution
To fix this immediate and critical crash risk, I have modified the function to allocate the
ModRepositoryFileInfoobjects on the heap usingnew. This ensures that the pointers passed via thefilesAvailablesignal remain valid after the function returns, preventing the crash.This is a minimal, targeted fix designed to resolve the most severe aspect of the bug without altering the existing
IModRepositoryBridgeinterface.Known Issues & Discussion
I understand that this solution is not perfect and introduces a new challenge: memory management. The
newly allocated objects are not currently deallocated, which will result in a memory leak.The ideal, long-term solution would likely involve changing the
IModRepositoryBridgeinterface to pass a list of objects by value (e.g.,QList<ModRepositoryFileInfo>) instead of pointers. However, as I am not deeply familiar with the overall architecture of this large project, I was hesitant to make such a significant change to a core interface.I am submitting this PR to solve the immediate crash risk and to open a discussion with the project maintainers. I would greatly appreciate your feedback and guidance on the best path forward.
Specifically, I would like to ask:
filesAvailablesignal? I would be happy to add the necessary memory deallocation logic (e.g.,qDeleteAll) there as a follow-up commit.Thank you for your time and for maintaining this great project!