Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/art/art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ T art<T>::set(const char *key, T value) {
if (root_ == nullptr) {
root_ = new leaf_node<T>(value);
root_->prefix_ = new char[key_len];
std::copy(key, key + key_len + 1, root_->prefix_);
std::copy(key, key + key_len, root_->prefix_);
root_->prefix_len_ = key_len;
return T{};
}
Expand Down Expand Up @@ -197,7 +197,7 @@ T art<T>::set(const char *key, T value) {
(**cur).prefix_len_ = old_prefix_len - prefix_match_len - 1;
std::copy(old_prefix + prefix_match_len + 1, old_prefix + old_prefix_len,
(**cur).prefix_);
delete old_prefix;
delete[] old_prefix;

auto new_node = new leaf_node<T>(value);
new_node->prefix_ = new char[key_len - depth - prefix_match_len - 1];
Expand Down
16 changes: 16 additions & 0 deletions test/art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ TEST_SUITE("art") {
}
}
}

SUBCASE("C-string semantics: string literals with explicit trailing nulls") {
/* Note: In C++, "aa\0" creates a 4-byte string literal: ['a', 'a', '\0', '\0']
* However, when passed as const char*, strlen() returns 2 (stops at first null).
* Therefore, both "aa" and "aa\0" result in the same key length (strlen + 1 = 3),
* making them identical keys from the ART's perspective.
* This is a fundamental limitation of C-string semantics, not a bug.
*/
trie.set("aa", &dummy_value_1);
REQUIRE_EQ(&dummy_value_1, trie.get("aa"));

/* Setting "aa\0" replaces "aa" because they're the same C-string */
int* prev = trie.set("aa\0", &dummy_value_2);
REQUIRE_EQ(&dummy_value_1, prev); /* Returns previous value */
REQUIRE_EQ(&dummy_value_2, trie.get("aa")); /* "aa" now has new value */
}
}

TEST_CASE("delete value") {
Expand Down