diff --git a/db/db_impl.cc b/db/db_impl.cc index 8f9c0168e..dcc90e31c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3502,6 +3502,9 @@ Status DBImpl::AddFile(ColumnFamilyHandle* column_family, auto cfh = reinterpret_cast(column_family); ColumnFamilyData* cfd = cfh->cfd(); + if (file_info->num_entries == 0) { + return Status::InvalidArgument("File contain no entries"); + } if (file_info->version != 1) { return Status::InvalidArgument("Generated table version is not supported"); } diff --git a/db/db_test.cc b/db/db_test.cc index 1143eef1b..6bef10ca9 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -8801,6 +8801,12 @@ TEST_F(DBTest, AddExternalSstFile) { ASSERT_EQ(file5_info.smallest_key, Key(400)); ASSERT_EQ(file5_info.largest_key, Key(499)); + // Cannot create an empty sst file + std::string file_empty = sst_files_folder + "file_empty.sst"; + ExternalSstFileInfo file_empty_info; + s = sst_file_writer.Finish(&file_empty_info); + ASSERT_NOK(s); + DestroyAndReopen(options); // Add file using file path s = db_->AddFile(file1); diff --git a/table/sst_file_writer.cc b/table/sst_file_writer.cc index 1c21a25f7..d13adbe08 100644 --- a/table/sst_file_writer.cc +++ b/table/sst_file_writer.cc @@ -163,6 +163,9 @@ Status SstFileWriter::Finish(ExternalSstFileInfo* file_info) { if (!r->builder) { return Status::InvalidArgument("File is not opened"); } + if (r->file_info.num_entries == 0) { + return Status::InvalidArgument("Cannot create sst file with no entries"); + } Status s = r->builder->Finish(); if (s.ok()) {