Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
- [feature] Add support for disjunctions in queries (`OR` queries).
- [fixed] Fixed stack overflow caused by deeply nested server timestamps.

# 10.6.0
- [fixed] Fix a potential high memory usage issue.
Expand Down
11 changes: 11 additions & 0 deletions Firestore/core/src/model/server_timestamp_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const char kServerTimestampSentinel[] = "server_timestamp";
Message<google_firestore_v1_Value> EncodeServerTimestamp(
const Timestamp& local_write_time,
absl::optional<google_firestore_v1_Value> previous_value) {
// We should avoid storing deeply nested server timestamp map values
// because we never use the intermediate "previous values".
// For example:
// previous: 42L, add: t1, result: t1 -> 42L
// previous: t1, add: t2, result: t2 -> 42L (NOT t2 -> t1 -> 42L)
// previous: t2, add: t3, result: t3 -> 42L (NOT t3 -> t2 -> t1 -> 42L)
// `getPreviousValue` recursively traverses server timestamps to find the
// least recent Value.
if (previous_value.has_value() && IsServerTimestamp(*previous_value)) {
previous_value = GetPreviousValue(*previous_value);
}
pb_size_t count = previous_value ? 3 : 2;

Message<google_firestore_v1_Value> result;
Expand Down
24 changes: 24 additions & 0 deletions Firestore/core/test/unit/local/local_store_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "Firestore/core/test/unit/local/local_store_test.h"

#include <thread> // NOLINT(build/c++11)
#include <unordered_map>
#include <utility>
#include <vector>
Expand All @@ -34,10 +35,14 @@
#include "Firestore/core/src/model/document.h"
#include "Firestore/core/src/model/document_key.h"
#include "Firestore/core/src/model/field_index.h"
#include "Firestore/core/src/model/field_mask.h"
#include "Firestore/core/src/model/field_path.h"
#include "Firestore/core/src/model/field_transform.h"
#include "Firestore/core/src/model/mutable_document.h"
#include "Firestore/core/src/model/mutation.h"
#include "Firestore/core/src/model/mutation_batch_result.h"
#include "Firestore/core/src/model/patch_mutation.h"
#include "Firestore/core/src/model/server_timestamp_util.h"
#include "Firestore/core/src/model/set_mutation.h"
#include "Firestore/core/src/model/transform_operation.h"
#include "Firestore/core/src/remote/existence_filter.h"
Expand Down Expand Up @@ -92,6 +97,7 @@ using testutil::Key;
using testutil::Map;
using testutil::OverlayTypeMap;
using testutil::Query;
using testutil::ServerTimestamp;
using testutil::UnknownDoc;
using testutil::UpdateRemoteEvent;
using testutil::UpdateRemoteEventWithLimboTargets;
Expand Down Expand Up @@ -1684,6 +1690,24 @@ TEST_P(LocalStoreTest, PatchMutationLeadsToPatchOverlay) {
OverlayTypeMap({{Key("foo/baz"), model::Mutation::Type::Patch}}));
}

TEST_P(LocalStoreTest, DeeplyNestedTimestampDoesNotCauseStackOverflow) {
Timestamp timestamp = Timestamp::Now();
Message<_google_firestore_v1_Value> initialServerTimestamp =
model::EncodeServerTimestamp(timestamp, absl::nullopt);
model::FieldPath path = model::FieldPath::FromDotSeparatedString("timestamp");
auto makeDeeplyNestedTimestamp = [&]() {
for (int i = 0; i < 1000; ++i) {
WriteMutation(testutil::MergeMutation(
"foo/bar",
Map("timestamp",
model::EncodeServerTimestamp(timestamp, *initialServerTimestamp)),
{path}, {ServerTimestamp("timestamp")}));
}
};
std::thread t(makeDeeplyNestedTimestamp);
EXPECT_NO_FATAL_FAILURE(t.join());
}

} // namespace local
} // namespace firestore
} // namespace firebase