aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-05-27 16:58:49 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-05-27 16:58:49 +0000
commita603138fbf5a743349ea4eb2efff33ee3c577a20 (patch)
tree906cf33f470d68add3cb0cc809ea250d42c88ec7
parent5af2a5190d06e0b378a75633e65b3919e75efb4e (diff)
parent1a80847b72a44761a7e2782871a3bc18f6b3f76b (diff)
downloadicing-android13-qpr1-s4-release.tar.gz
Change-Id: If7c7532faf625c7960b13064d0ee76267bf5202f
-rw-r--r--icing/file/file-backed-vector.h5
-rw-r--r--icing/file/file-backed-vector_test.cc21
2 files changed, 25 insertions, 1 deletions
diff --git a/icing/file/file-backed-vector.h b/icing/file/file-backed-vector.h
index 00bdc7e..7e42e32 100644
--- a/icing/file/file-backed-vector.h
+++ b/icing/file/file-backed-vector.h
@@ -586,8 +586,11 @@ libtextclassifier3::Status FileBackedVector<T>::GrowIfNecessary(
}
int64_t current_file_size = filesystem_->GetFileSize(file_path_.c_str());
- int64_t least_file_size_needed = sizeof(Header) + num_elements * sizeof(T);
+ if (current_file_size == Filesystem::kBadFileSize) {
+ return absl_ports::InternalError("Unable to retrieve file size.");
+ }
+ int64_t least_file_size_needed = sizeof(Header) + num_elements * sizeof(T);
if (least_file_size_needed <= current_file_size) {
// Our underlying file can hold the target num_elements cause we've grown
// before
diff --git a/icing/file/file-backed-vector_test.cc b/icing/file/file-backed-vector_test.cc
index 54f9ef5..ed94fa5 100644
--- a/icing/file/file-backed-vector_test.cc
+++ b/icing/file/file-backed-vector_test.cc
@@ -677,6 +677,27 @@ TEST_F(FileBackedVectorTest, RemapFailureStillValidInstance) {
EXPECT_THAT(vector->Get(kResizingIndex / 2), IsOkAndHolds(Pointee(Eq(9))));
}
+TEST_F(FileBackedVectorTest, BadFileSizeDuringGrowReturnsError) {
+ auto mock_filesystem = std::make_unique<MockFilesystem>();
+ ICING_ASSERT_OK_AND_ASSIGN(
+ std::unique_ptr<FileBackedVector<int>> vector,
+ FileBackedVector<int>::Create(
+ *mock_filesystem, file_path_,
+ MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC));
+
+ // At first, the vector is empty and has no mapping established. The first Set
+ // call will cause a Grow.
+ // During Grow, we will attempt to check the underlying file size to see if
+ // growing is actually necessary. Return an error on the call to GetFileSize.
+ ON_CALL(*mock_filesystem, GetFileSize(A<const char*>()))
+ .WillByDefault(Return(Filesystem::kBadFileSize));
+
+ // We should fail gracefully and return an INTERNAL error to indicate that
+ // there was an issue retrieving the file size.
+ EXPECT_THAT(vector->Set(0, 7),
+ StatusIs(libtextclassifier3::StatusCode::INTERNAL));
+}
+
} // namespace
} // namespace lib