aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler
diff options
context:
space:
mode:
authorGravatar Adam Cozzette <acozzette@google.com>2017-07-19 15:24:30 -0700
committerGravatar Adam Cozzette <acozzette@google.com>2017-07-19 15:29:28 -0700
commit417aae615489f6b6a0aa750a4c3b4affe597d923 (patch)
treeb74874618a3eb076cb87f5f527ad3c18012c989f /src/google/protobuf/compiler
parentaa61bb0d3cee089c37da32584d03132177dd847c (diff)
Fixed dynamic initialization for C++ lite
An ifdef condition seems to have been inverted by mistake, causing the dynamic initialization to occur for lite if and only if the _NO_STATIC_INITIALIZER macro is set. This problem manifested itself as segfaults due to uninitialized empty strings: https://github.com/google/protobuf/issues/2839 Since no one complained about initialization not happening, it would appear that we can just disable this initialization for lite unconditionally, so that is what this change does. Instead of the default instance initialization happening pre-main, it now always happens lazily when needed.
Diffstat (limited to 'src/google/protobuf/compiler')
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_file.cc31
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_message.cc4
-rw-r--r--src/google/protobuf/compiler/plugin.pb.cc2
-rw-r--r--src/google/protobuf/compiler/profile.pb.cc2
4 files changed, 15 insertions, 24 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc
index 1f7a66c5..d79a3e73 100644
--- a/src/google/protobuf/compiler/cpp/cpp_file.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_file.cc
@@ -526,11 +526,10 @@ class FileGenerator::ForwardDeclarations {
void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
// AddDescriptors() is a file-level procedure which adds the encoded
// FileDescriptorProto for this .proto file to the global DescriptorPool for
- // generated files (DescriptorPool::generated_pool()). It either runs at
- // static initialization time (by default) or when default_instance() is
- // called for the first time (in LITE_RUNTIME mode with
- // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER flag enabled). This procedure also
- // constructs default instances and registers extensions.
+ // generated files (DescriptorPool::generated_pool()). It ordinarily runs at
+ // static initialization time, but is not used at all in LITE_RUNTIME mode
+ // except when extensions are used. This procedure also constructs default
+ // instances and registers extensions.
//
// Its sibling, AssignDescriptors(), actually pulls the compiled
// FileDescriptor from the DescriptorPool and uses it to populate all of
@@ -889,19 +888,15 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
" ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);\n"
"}\n");
- if (!StaticInitializersForced(file_, options_)) {
- printer->Print("#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n");
- }
- printer->Print(
- // With static initializers.
- "// Force AddDescriptors() to be called at static initialization time.\n"
- "struct StaticDescriptorInitializer {\n"
- " StaticDescriptorInitializer() {\n"
- " AddDescriptors();\n"
- " }\n"
- "} static_descriptor_initializer;\n");
- if (!StaticInitializersForced(file_, options_)) {
- printer->Print("#endif // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n");
+ if (StaticInitializersForced(file_, options_)) {
+ printer->Print(
+ "// Force AddDescriptors() to be called at dynamic initialization "
+ "time.\n"
+ "struct StaticDescriptorInitializer {\n"
+ " StaticDescriptorInitializer() {\n"
+ " AddDescriptors();\n"
+ " }\n"
+ "} static_descriptor_initializer;\n");
}
}
diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc
index 8ff03b39..33231ae3 100644
--- a/src/google/protobuf/compiler/cpp/cpp_message.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_message.cc
@@ -2411,11 +2411,7 @@ GenerateStructors(io::Printer* printer) {
printer->Print(
"$classname$::$classname$(::google::protobuf::Arena* arena)\n"
" : $initializer$ {\n"
- // When arenas are used it's safe to assume we have finished
- // static init time (protos with arenas are unsafe during static init)
- "#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n"
" $file_namespace$::InitDefaults();\n"
- "#endif // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n"
" SharedCtor();\n"
" RegisterArenaDtor(arena);\n"
" // @@protoc_insertion_point(arena_constructor:$full_name$)\n"
diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc
index 8846bd74..3842ef61 100644
--- a/src/google/protobuf/compiler/plugin.pb.cc
+++ b/src/google/protobuf/compiler/plugin.pb.cc
@@ -201,7 +201,7 @@ void AddDescriptors() {
static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);
}
-// Force AddDescriptors() to be called at static initialization time.
+// Force AddDescriptors() to be called at dynamic initialization time.
struct StaticDescriptorInitializer {
StaticDescriptorInitializer() {
AddDescriptors();
diff --git a/src/google/protobuf/compiler/profile.pb.cc b/src/google/protobuf/compiler/profile.pb.cc
index 7bc6ab50..bf69f79d 100644
--- a/src/google/protobuf/compiler/profile.pb.cc
+++ b/src/google/protobuf/compiler/profile.pb.cc
@@ -167,7 +167,7 @@ void AddDescriptors() {
static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);
}
-// Force AddDescriptors() to be called at static initialization time.
+// Force AddDescriptors() to be called at dynamic initialization time.
struct StaticDescriptorInitializer {
StaticDescriptorInitializer() {
AddDescriptors();