diff --git a/client/crash_report_database_test.cc b/client/crash_report_database_test.cc index 54dcdd75..ca839f47 100644 --- a/client/crash_report_database_test.cc +++ b/client/crash_report_database_test.cc @@ -555,6 +555,36 @@ TEST_F(CrashReportDatabaseTest, DeleteReport) { db()->LookUpCrashReport(keep_completed.uuid, &keep_completed)); } +TEST_F(CrashReportDatabaseTest, DeleteReportEmptyingDatabase) { + CrashReportDatabase::Report report; + CreateCrashReport(&report); + + EXPECT_TRUE(FileExists(report.file_path)); + + UploadReport(report.uuid, true, "1"); + + EXPECT_EQ(CrashReportDatabase::kNoError, + db()->LookUpCrashReport(report.uuid, &report)); + + EXPECT_TRUE(FileExists(report.file_path)); + + // This causes an empty database to be written, make sure this is handled. + EXPECT_EQ(CrashReportDatabase::kNoError, db()->DeleteReport(report.uuid)); + EXPECT_FALSE(FileExists(report.file_path)); +} + +TEST_F(CrashReportDatabaseTest, ReadEmptyDatabase) { + CrashReportDatabase::Report report; + CreateCrashReport(&report); + EXPECT_EQ(CrashReportDatabase::kNoError, db()->DeleteReport(report.uuid)); + + // Deleting and the creating another report causes an empty database to be + // loaded. Make sure this is handled. + + CrashReportDatabase::Report report2; + CreateCrashReport(&report2); +} + } // namespace } // namespace test } // namespace crashpad diff --git a/client/crash_report_database_win.cc b/client/crash_report_database_win.cc index 7f4ede4f..fff340af 100644 --- a/client/crash_report_database_win.cc +++ b/client/crash_report_database_win.cc @@ -417,26 +417,29 @@ void Metadata::Read() { return; } - std::vector records(header.num_records); - if (!LoggingReadFile(handle_.get(), &records[0], records_size.ValueOrDie())) { - LOG(ERROR) << "failed to read records"; - return; - } - - std::string string_table = ReadRestOfFileAsString(handle_.get()); - if (string_table.empty() || string_table.back() != '\0') { - LOG(ERROR) << "bad string table"; - return; - } - std::vector reports; - for (const auto& record : records) { - if (record.file_path_index >= string_table.size() || - record.id_index >= string_table.size()) { - LOG(ERROR) << "invalid string table index"; + if (header.num_records > 0) { + std::vector records(header.num_records); + if (!LoggingReadFile( + handle_.get(), &records[0], records_size.ValueOrDie())) { + LOG(ERROR) << "failed to read records"; return; } - reports.push_back(ReportDisk(record, report_dir_, string_table)); + + std::string string_table = ReadRestOfFileAsString(handle_.get()); + if (string_table.empty() || string_table.back() != '\0') { + LOG(ERROR) << "bad string table"; + return; + } + + for (const auto& record : records) { + if (record.file_path_index >= string_table.size() || + record.id_index >= string_table.size()) { + LOG(ERROR) << "invalid string table index"; + return; + } + reports.push_back(ReportDisk(record, report_dir_, string_table)); + } } reports_.swap(reports); } @@ -466,6 +469,9 @@ void Metadata::Write() { return; } + if (num_records == 0) + return; + // Build the records and string table we're going to write. std::string string_table; std::vector records;