Skip to content

Commit

Permalink
Fixing unsafe globals
Browse files Browse the repository at this point in the history
  • Loading branch information
mkruskal-google committed Aug 16, 2022
1 parent e5d88e7 commit 49564d0
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,17 @@ static const char* const kKeywordList[] = {
#endif // !PROTOBUF_FUTURE_BREAKING_CHANGES
};

static absl::flat_hash_set<std::string>* MakeKeywordsMap() {
auto* result = new absl::flat_hash_set<std::string>();
for (const auto keyword : kKeywordList) {
result->emplace(keyword);
}
return result;
}
const absl::flat_hash_set<std::string>& Keywords() {
static const auto* keywords = []{
auto* keywords = new absl::flat_hash_set<std::string>();

static absl::flat_hash_set<std::string>& 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";
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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("_");
Expand Down Expand Up @@ -1116,7 +1118,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) {
}

bool IsWellKnownMessage(const FileDescriptor* file) {
static const absl::flat_hash_set<std::string> well_known_files{
static const auto* well_known_files = new absl::flat_hash_set<std::string>{
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/compiler/plugin.proto",
Expand All @@ -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,
Expand Down

0 comments on commit 49564d0

Please sign in to comment.