Skip to content

Commit

Permalink
Use correct DefinitionKey length
Browse files Browse the repository at this point in the history
Summary:
We need to provide the length of the final string, which is the same
as the length of the string before escaping, rather than the length of the
escaped string.
Also avoid recomputing the string by exposing the size directly.

Reviewed By: yoney

Differential Revision: D67982408

fbshipit-source-id: 3b671f6358748de2ecb29ee56bbfcfd7cbc0538d
  • Loading branch information
iahs authored and facebook-github-bot committed Jan 9, 2025
1 parent 87da468 commit 5e2b91f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions thrift/compiler/generate/t_mstch_cpp2_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,7 @@ class cpp_mstch_service : public mstch_service {
return escape_binary_string(s.identify_definition(*service_));
}
mstch::node definition_key_length() {
schematizer s(*service_->program()->scope(), sm_, {});
return escape_binary_string(s.identify_definition(*service_)).length();
return schematizer::definition_identifier_length();
}

private:
Expand Down
8 changes: 6 additions & 2 deletions thrift/compiler/sema/schematizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,10 @@ std::string_view schematizer::program_checksum(const t_program& program) {
std::string(reinterpret_cast<const char*>(hash), sizeof(hash)));
}

size_t schematizer::definition_identifier_length() {
return 16;
}

std::string schematizer::identify_definition(const t_named& node) {
// @lint-ignore CLANGTIDY facebook-hte-CArray
unsigned char hash[SHA256_DIGEST_LENGTH];
Expand All @@ -958,8 +962,8 @@ std::string schematizer::identify_definition(const t_named& node) {
node.program()->path(),
node.name());
SHA256(reinterpret_cast<const unsigned char*>(val.c_str()), val.size(), hash);
constexpr size_t num_bytes = 16;
return std::string(reinterpret_cast<const char*>(hash), num_bytes);
return std::string(
reinterpret_cast<const char*>(hash), definition_identifier_length());
}

int64_t schematizer::identify_program(const t_program& node) {
Expand Down
1 change: 1 addition & 0 deletions thrift/compiler/sema/schematizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class schematizer {
// across runs on different including programs.
std::string identify_definition(const t_named& node);
int64_t identify_program(const t_program& node);
static size_t definition_identifier_length();

// Get the name of the program's schema const.
static std::string name_schema(source_manager& sm, const t_program& node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apache::thrift::ServiceHandler<::cpp2::PrimitivesService>::CreateMethodMetadataR
std::optional<apache::thrift::schema::DefinitionsSchema> apache::thrift::ServiceHandler<::cpp2::PrimitivesService>::getServiceSchema() {
std::optional<apache::thrift::schema::DefinitionsSchema> ret = schema::DefinitionsSchema{};
ret->schema = ::apache::thrift::SchemaRegistry::mergeSchemas(::cpp2::module_constants::_fbthrift_schema_b747839c13cb3aa5_includes());
ret->definitions = { {"\x0d\x05\x1d\xbc\xb2\xd5\x1d\x8f\x95\x45\xbb\x51\xfa\xcf\x0f\xfe", 64} };
ret->definitions = { {"\x0d\x05\x1d\xbc\xb2\xd5\x1d\x8f\x95\x45\xbb\x51\xfa\xcf\x0f\xfe", 16} };
return ret;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apache::thrift::ServiceHandler<::cpp2::PrimitivesService>::CreateMethodMetadataR
std::optional<apache::thrift::schema::DefinitionsSchema> apache::thrift::ServiceHandler<::cpp2::PrimitivesService>::getServiceSchema() {
std::optional<apache::thrift::schema::DefinitionsSchema> ret = schema::DefinitionsSchema{};
ret->schema = ::apache::thrift::SchemaRegistry::mergeSchemas(::cpp2::module_constants::_fbthrift_schema_b747839c13cb3aa5_includes());
ret->definitions = { {"\x0d\x05\x1d\xbc\xb2\xd5\x1d\x8f\x95\x45\xbb\x51\xfa\xcf\x0f\xfe", 64} };
ret->definitions = { {"\x0d\x05\x1d\xbc\xb2\xd5\x1d\x8f\x95\x45\xbb\x51\xfa\xcf\x0f\xfe", 16} };
return ret;
}
#endif
Expand Down

0 comments on commit 5e2b91f

Please sign in to comment.