Index: third_party/protobuf/src/google/protobuf/util/type_resolver_util.cc |
diff --git a/third_party/protobuf/src/google/protobuf/util/type_resolver_util.cc b/third_party/protobuf/src/google/protobuf/util/type_resolver_util.cc |
index febaa41f75004aadbaa465cdab2b69c2e982634b..963939032a8c723065f05a99c96fb87b26f6f991 100644 |
--- a/third_party/protobuf/src/google/protobuf/util/type_resolver_util.cc |
+++ b/third_party/protobuf/src/google/protobuf/util/type_resolver_util.cc |
@@ -54,6 +54,17 @@ using util::Status; |
using util::error::INVALID_ARGUMENT; |
using util::error::NOT_FOUND; |
+bool SplitTypeUrl(const string& type_url, string* url_prefix, |
+ string* message_name) { |
+ size_t pos = type_url.find_last_of("/"); |
+ if (pos == string::npos) { |
+ return false; |
+ } |
+ *url_prefix = type_url.substr(0, pos); |
+ *message_name = type_url.substr(pos + 1); |
+ return true; |
+} |
+ |
class DescriptorPoolTypeResolver : public TypeResolver { |
public: |
DescriptorPoolTypeResolver(const string& url_prefix, |
@@ -61,27 +72,38 @@ class DescriptorPoolTypeResolver : public TypeResolver { |
: url_prefix_(url_prefix), pool_(pool) {} |
Status ResolveMessageType(const string& type_url, Type* type) { |
- string type_name; |
- Status status = ParseTypeUrl(type_url, &type_name); |
- if (!status.ok()) { |
- return status; |
+ string url_prefix, message_name; |
+ if (!SplitTypeUrl(type_url, &url_prefix, &message_name) || |
+ url_prefix != url_prefix_) { |
+ return Status(INVALID_ARGUMENT, |
+ StrCat("Invalid type URL, type URLs must be of the form '", |
+ url_prefix_, "/<typename>', got: ", type_url)); |
} |
- |
- const Descriptor* descriptor = pool_->FindMessageTypeByName(type_name); |
+ if (url_prefix != url_prefix_) { |
+ return Status(INVALID_ARGUMENT, |
+ "Cannot resolve types from URL: " + url_prefix); |
+ } |
+ const Descriptor* descriptor = pool_->FindMessageTypeByName(message_name); |
if (descriptor == NULL) { |
- return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); |
+ return Status(NOT_FOUND, |
+ "Invalid type URL, unknown type: " + message_name); |
} |
ConvertDescriptor(descriptor, type); |
return Status(); |
} |
Status ResolveEnumType(const string& type_url, Enum* enum_type) { |
- string type_name; |
- Status status = ParseTypeUrl(type_url, &type_name); |
- if (!status.ok()) { |
- return status; |
+ string url_prefix, type_name; |
+ if (!SplitTypeUrl(type_url, &url_prefix, &type_name) || |
+ url_prefix != url_prefix_) { |
+ return Status(INVALID_ARGUMENT, |
+ StrCat("Invalid type URL, type URLs must be of the form '", |
+ url_prefix_, "/<typename>', got: ", type_url)); |
+ } |
+ if (url_prefix != url_prefix_) { |
+ return Status(INVALID_ARGUMENT, |
+ "Cannot resolve types from URL: " + url_prefix); |
} |
- |
const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); |
if (descriptor == NULL) { |
return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); |
@@ -181,16 +203,6 @@ class DescriptorPoolTypeResolver : public TypeResolver { |
return url_prefix_ + "/" + descriptor->full_name(); |
} |
- Status ParseTypeUrl(const string& type_url, string* type_name) { |
- if (type_url.substr(0, url_prefix_.size() + 1) != url_prefix_ + "/") { |
- return Status(INVALID_ARGUMENT, |
- StrCat("Invalid type URL, type URLs must be of the form '", |
- url_prefix_, "/<typename>', got: ", type_url)); |
- } |
- *type_name = type_url.substr(url_prefix_.size() + 1); |
- return Status(); |
- } |
- |
string DefaultValueAsString(const FieldDescriptor* descriptor) { |
switch (descriptor->cpp_type()) { |
case FieldDescriptor::CPPTYPE_INT32: |