Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Firestore/core/src/bundle/bundle_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ ResourcePath BundleSerializer::DecodeName(JsonReader& reader,
}
auto path =
ResourcePath::FromString(document_name.get_ref<const std::string&>());
if (!rpc_serializer_.IsLocalResourceName(path)) {
if (!rpc_serializer_.IsLocalResourceName(path) || path.size() < 5 ||
path[4] != "documents") {
Comment thread
cherylEnkidu marked this conversation as resolved.
reader.Fail("Resource name is not valid for current instance: " +
path.CanonicalString());
return {};
Expand Down
3 changes: 2 additions & 1 deletion Firestore/core/src/remote/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,8 @@ bool Serializer::IsLocalResourceName(const ResourcePath& path) const {

bool Serializer::IsLocalDocumentKey(absl::string_view path) const {
auto resource = ResourcePath::FromStringView(path);
return IsLocalResourceName(resource) &&
return IsLocalResourceName(resource) && resource.size() >= 5 &&
resource[4] == "documents" &&
DocumentKey::IsDocumentKey(resource.PopFirst(5));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation calls DocumentKey::IsDocumentKey(resource.PopFirst(5)). PopFirst allocates a new ResourcePath vector and copies all segment strings in memory. This allocation is unnecessary because we only need to verify if the number of segments remaining after the database prefix is even.

Suggested Diff

bool Serializer::IsLocalDocumentKey(absl::string_view path) const {
  auto resource = ResourcePath::FromStringView(path);
  return IsLocalResourceName(resource) && resource.size() >= 5 &&
         resource[4] == "documents" &&
-        DocumentKey::IsDocumentKey(resource.PopFirst(5));
+        (resource.size() - 5) % 2 == 0;
}

}

Expand Down
Loading