SeekToFirst() on non-exsiting collection cause segmentation fault
karczex opened this issue · comments
In the code below I create iterator to the collection before adding any element to it by SSet() method.
TEST_F(EngineBasicTest, TestSeekToFirst) {
const std::string empty_collection = "Empty";
ASSERT_EQ(Engine::Open(db_path.c_str(), &engine, configs, stdout),
Status::Ok);
auto iter = engine->NewSortedIterator(empty_collection);
iter->SeekToFirst();
ASSERT_FALSE(iter->Valid());
}
As Collection would be implicitly created on first SSet()
it do not exists during SeekToFirst()
call. In that case iter->Valid()
should gracefully return false, as such iterator obviously is invalid. Instead of that it cause segmentation fault.
Test output:
localhost/kvdk:build# PMEM_IS_PMEM_FORCE=1 ./dbtest --gtest_filter="*SeekToFirst*"
Note: Google Test filter = *SeekToFirst*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from EngineBasicTest
[ RUN ] EngineBasicTest.TestSeekToFirst
[LOG] time 0 ms: Initializing PMEM size 17179869184 in file /mnt/pmem0/data/data
[LOG] time 2087 ms: Map pmem space done
[LOG] time 2091 ms: In restoring: iterated 0 records
Segmentation fault (core dumped)
Hi karczex, Thank you for reporting this issue.
You need to check if the iter is not null before using it:
auto iter = engine->NewSortedIterator(empty_collection);
if (iter) {
iter->SeekToFirst();
ASSERT_FALSE(iter->Valid());
}
And your finding reminds us to add checking for "iter" in the tests.cpp, tutorial.cpp and bench.cpp. 👍
Thanks,
Peifeng
Thanks for quick response :)