Fix buffer overflow and memory leak in set method#30
Merged
rafaelkallis merged 3 commits intomainfrom Nov 24, 2025
Merged
Conversation
Co-authored-by: rafaelkallis <9661903+rafaelkallis@users.noreply.github.com>
Co-authored-by: rafaelkallis <9661903+rafaelkallis@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix key insertion logic in art class
Fix buffer overflow and memory leak in set method
Nov 24, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes two critical bugs in the set method: a buffer overflow writing beyond allocated memory and a memory leak from incorrect delete operator usage. The fixes ensure correct memory allocation/deallocation patterns and add test coverage documenting C-string semantics behavior.
- Fixed buffer overflow by copying exactly
key_lenbytes instead ofkey_len + 1into akey_len-sized buffer (line 126) - Fixed memory leak by using
delete[]instead ofdeletefor array deallocation (line 200) - Added test case documenting that
"aa"and"aa\0"are identical keys due to C-string semantics
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| include/art/art.hpp | Fixes buffer overflow in root node creation (line 126) and memory leak in prefix reallocation (line 200) |
| test/art.cpp | Adds test case documenting C-string semantics for keys with explicit trailing nulls |
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.
The
setmethod had a buffer overflow writing one byte beyond allocated memory and a memory leak using the wrong delete operator for arrays.Changes
Buffer overflow fix (line 126)
Memory leak fix (line 200)
Note on reported "aa\0" behavior
The issue description expected
"aa"and"aa\0"to be distinct keys, but they're identical from the API's perspective:This is expected C-string semantics. The string literal
"aa\0"contains['a','a','\0','\0'], butstrlen()stops at the first null. Supporting embedded nulls would require accepting explicit lengths, a breaking API change.Added test case documenting this behavior.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.