From ba5a02169e1503c4164acecd35789f2b2a4ce375 Mon Sep 17 00:00:00 2001 From: Kamil Braun Date: Fri, 12 Jul 2019 16:36:02 +0200 Subject: [PATCH] Fix segmentation fault when querying system.size_estimates for an empty keyspace. --- db/size_estimates_virtual_reader.cc | 9 ++++++++- tests/virtual_reader_test.cc | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/db/size_estimates_virtual_reader.cc b/db/size_estimates_virtual_reader.cc index 912a4b8f87..f597f3e046 100644 --- a/db/size_estimates_virtual_reader.cc +++ b/db/size_estimates_virtual_reader.cc @@ -83,7 +83,14 @@ public: , _ranges(std::ref(ranges)) , _cf_names_idx(cf_names.size()) , _ranges_idx(ranges.size()) - { } + { + if (cf_names.empty() || ranges.empty()) { + // The product of an empty range with any range is an empty range. + // In this case we want the end iterator to be equal to the begin iterator, + // which has_ranges_idx = _cf_names_idx = 0. + _ranges_idx = _cf_names_idx = 0; + } + } virtual_row_iterator& operator++() { if (++_ranges_idx == _ranges.get().size() && ++_cf_names_idx < _cf_names.get().size()) { _ranges_idx = 0; diff --git a/tests/virtual_reader_test.cc b/tests/virtual_reader_test.cc index 3e8a67121f..a4987381a9 100644 --- a/tests/virtual_reader_test.cc +++ b/tests/virtual_reader_test.cc @@ -52,10 +52,13 @@ SEASTAR_TEST_CASE(test_query_size_estimates_virtual_table) { auto end_token1 = utf8_type->to_string(ranges[3].end); auto end_token2 = utf8_type->to_string(ranges[55].end); + auto rs = e.execute_cql("select * from system.size_estimates where keyspace_name = 'ks';").get0(); + assert_that(rs).is_rows().with_size(0); + e.execute_cql("create table cf1(pk text PRIMARY KEY, v int);").discard_result().get(); e.execute_cql("create table cf2(pk text PRIMARY KEY, v int);").discard_result().get(); - auto rs = e.execute_cql("select * from system.size_estimates where keyspace_name = 'ks';").get0(); + rs = e.execute_cql("select * from system.size_estimates where keyspace_name = 'ks';").get0(); assert_that(rs).is_rows().with_size(512); rs = e.execute_cql("select * from system.size_estimates where keyspace_name = 'ks' limit 100;").get0();