Potential fix for code scanning alert no. 6: Mismatching new/free or malloc/delete#34
Merged
amikhail48 merged 1 commit intomainfrom Mar 4, 2026
Merged
Potential fix for code scanning alert no. 6: Mismatching new/free or malloc/delete#34amikhail48 merged 1 commit intomainfrom
amikhail48 merged 1 commit intomainfrom
Conversation
…malloc/delete Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.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.
Potential fix for https://github.com/RunEdgeAI/coreflow/security/code-scanning/6
In general, to fix a mismatched allocation/deallocation, you must pair
malloc/calloc/reallocwithfree, andnew/new[]withdelete/delete[]. You should use the same mechanism for all allocations and deallocations of a given buffer.For this specific case,
data_addris allocated viaallocateScalarMemory(size). CodeQL reports that this ultimately usesnew[], so deallocation must usedelete[]instead offree. The logic in theVX_WRITE_ONLYcase reallocates whendata_len < sizeby saving the old pointer intmp_addr, callingallocateScalarMemory(size)to allocate a new buffer, and if successful, freeing the old buffer. The only incorrect operation is usingfree(tmp_addr);to release that old buffer. We should replace it withdelete[] tmp_addr;, performing the correct deallocation while preserving the rest of the behavior.Concretely, in
framework/src/vx_scalar.cpp, in thecase VX_WRITE_ONLY:branch wheredata_len < size, change line 271 fromfree(tmp_addr);todelete[] static_cast<vx_uint8*>(tmp_addr);(ordelete[] tmp_addr;ifdata_addris known to be avx_uint8*or similar array type). Since we only seevoid *here, a safe and explicit fix is to cast back to the element type used for allocation. Assumingdata_addr/tmp_addractually hold avx_uint8[](common for scalar storage), we can cast tovx_uint8*before deleting withdelete[]. No new headers are needed;delete[]is built into C++.Suggested fixes powered by Copilot Autofix. Review carefully before merging.