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 9a9ba26
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +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);
const absl::flat_hash_set<std::string>& Keywords() {
static auto* keywords = []{
auto keywords = new absl::flat_hash_set<std::string>();

for (const auto keyword : kKeywordList) {
keywords->emplace(keyword);
}
}
return result;
return *keywords;
}

static absl::flat_hash_set<std::string>& kKeywords = *MakeKeywordsMap();

std::string IntTypeName(const Options& options, const std::string& type) {
return type + "_t";
}
Expand Down Expand Up @@ -509,7 +510,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 +519,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 +553,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 +864,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 +1117,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 +1131,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 9a9ba26

Please sign in to comment.