Skip to content

Commit 870406f

Browse files
committed
try to fix GetSelf invalid access
1 parent a83090a commit 870406f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/types/redis_bloom_chain.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ rocksdb::Status BloomChain::InsertCommon(engine::Context &ctx, const Slice &user
204204
return slice;
205205
};
206206
auto strip_string_from_pinnable_slice = [](rocksdb::PinnableSlice &slice) -> std::string {
207-
if (slice.GetSelf() != nullptr) {
207+
if (!slice.IsPinned()) {
208+
// Only a "PinSelf" slice ( which is !IsPinned )
209+
// can operate in this way.
208210
return std::move(*slice.GetSelf());
209211
}
210212
return slice.ToString();

tests/cppunit/types/bloom_chain_test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <gtest/gtest.h>
2222

2323
#include <memory>
24+
#include <random>
2425

2526
#include "test_base.h"
2627
#include "types/redis_bloom_chain.h"
@@ -111,3 +112,36 @@ TEST_F(RedisBloomChainTest, DuplicateInsert) {
111112
s = sb_chain_->MAdd(*ctx_, key_, arrays, &results);
112113
EXPECT_TRUE(s.ok());
113114
}
115+
116+
TEST_F(RedisBloomChainTest, MultiThreadInsert) {
117+
// Concurrent insert, every task would insert 100 random values
118+
std::mutex mu;
119+
120+
std::default_random_engine gen(42);
121+
std::uniform_int_distribution<uint32_t> dist;
122+
auto insert_task = [&]() {
123+
// Generate 100 random keys
124+
std::vector<std::string> arrays;
125+
for (size_t idx = 0; idx < 100; ++idx) {
126+
arrays.push_back("itemx" + std::to_string(dist(gen)));
127+
}
128+
engine::Context ctx(storage_.get());
129+
// Call madd
130+
std::vector<redis::BloomFilterAddResult> results;
131+
results.resize(arrays.size());
132+
auto s = sb_chain_->MAdd(ctx, key_, arrays, &results);
133+
EXPECT_TRUE(s.ok());
134+
};
135+
136+
std::vector<std::thread> threads;
137+
threads.reserve(10);
138+
for (int32_t thread_idx = 0; thread_idx < 10; ++thread_idx) {
139+
threads.emplace_back([&]() {
140+
std::lock_guard<std::mutex> lock(mu);
141+
insert_task();
142+
});
143+
}
144+
for (auto& thread : threads) {
145+
thread.join();
146+
}
147+
}

0 commit comments

Comments
 (0)