Skip to content

Potential fix for code scanning alert no. 6: Mismatching new/free or malloc/delete#34

Merged
amikhail48 merged 1 commit intomainfrom
alert-autofix-6
Mar 4, 2026
Merged

Potential fix for code scanning alert no. 6: Mismatching new/free or malloc/delete#34
amikhail48 merged 1 commit intomainfrom
alert-autofix-6

Conversation

@amikhail48
Copy link
Member

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/realloc with free, and new/new[] with delete/delete[]. You should use the same mechanism for all allocations and deallocations of a given buffer.

For this specific case, data_addr is allocated via allocateScalarMemory(size). CodeQL reports that this ultimately uses new[], so deallocation must use delete[] instead of free. The logic in the VX_WRITE_ONLY case reallocates when data_len < size by saving the old pointer in tmp_addr, calling allocateScalarMemory(size) to allocate a new buffer, and if successful, freeing the old buffer. The only incorrect operation is using free(tmp_addr); to release that old buffer. We should replace it with delete[] tmp_addr;, performing the correct deallocation while preserving the rest of the behavior.

Concretely, in framework/src/vx_scalar.cpp, in the case VX_WRITE_ONLY: branch where data_len < size, change line 271 from free(tmp_addr); to delete[] static_cast<vx_uint8*>(tmp_addr); (or delete[] tmp_addr; if data_addr is known to be a vx_uint8* or similar array type). Since we only see void * here, a safe and explicit fix is to cast back to the element type used for allocation. Assuming data_addr/tmp_addr actually hold a vx_uint8[] (common for scalar storage), we can cast to vx_uint8* before deleting with delete[]. No new headers are needed; delete[] is built into C++.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…malloc/delete

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@amikhail48 amikhail48 marked this pull request as ready for review March 4, 2026 00:45
@amikhail48 amikhail48 merged commit e4d037d into main Mar 4, 2026
7 checks passed
@amikhail48 amikhail48 deleted the alert-autofix-6 branch March 4, 2026 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant