From 49564d0c7e51758b0d452a56e2f32c12522420fa Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:42:24 -0700 Subject: [PATCH] Fixing unsafe globals --- src/google/protobuf/compiler/cpp/helpers.cc | 30 +++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index d36967f7dcb6..91351596dac8 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -176,15 +176,17 @@ static const char* const kKeywordList[] = { #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES }; -static absl::flat_hash_set* MakeKeywordsMap() { - auto* result = new absl::flat_hash_set(); - for (const auto keyword : kKeywordList) { - result->emplace(keyword); - } - return result; -} +const absl::flat_hash_set& Keywords() { + static const auto* keywords = []{ + auto* keywords = new absl::flat_hash_set(); -static absl::flat_hash_set& kKeywords = *MakeKeywordsMap(); + for (const auto keyword : kKeywordList) { + keywords->emplace(keyword); + } + return keywords; + }(); + return *keywords; +} std::string IntTypeName(const Options& options, const std::string& type) { return type + "_t"; @@ -509,7 +511,7 @@ std::string SuperClassName(const Descriptor* descriptor, } std::string ResolveKeyword(const std::string& name) { - if (kKeywords.count(name) > 0) { + if (Keywords().count(name) > 0) { return name + "_"; } return name; @@ -518,7 +520,7 @@ std::string ResolveKeyword(const std::string& name) { std::string FieldName(const FieldDescriptor* field) { std::string result = field->name(); LowerString(&result); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -552,7 +554,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) { std::string EnumValueName(const EnumValueDescriptor* enum_value) { std::string result = enum_value->name(); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -863,7 +865,7 @@ std::string SafeFunctionName(const Descriptor* descriptor, // Single underscore will also make it conflicting with the private data // member. We use double underscore to escape function names. function_name.append("__"); - } else if (kKeywords.count(name) > 0) { + } else if (Keywords().count(name) > 0) { // If the field name is a keyword, we append the underscore back to keep it // consistent with other function names. function_name.append("_"); @@ -1116,7 +1118,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) { } bool IsWellKnownMessage(const FileDescriptor* file) { - static const absl::flat_hash_set well_known_files{ + static const auto* well_known_files = new absl::flat_hash_set{ "google/protobuf/any.proto", "google/protobuf/api.proto", "google/protobuf/compiler/plugin.proto", @@ -1130,7 +1132,7 @@ bool IsWellKnownMessage(const FileDescriptor* file) { "google/protobuf/type.proto", "google/protobuf/wrappers.proto", }; - return well_known_files.find(file->name()) != well_known_files.end(); + return well_known_files->find(file->name()) != well_known_files->end(); } static void GenerateUtf8CheckCode(const FieldDescriptor* field,