From 7747a0703253e879a53d0ab8a92304b8dff97788 Mon Sep 17 00:00:00 2001
From: David Raila <raila@illinois.edu>
Date: Fri, 25 Jul 2008 04:38:05 +0000
Subject: [PATCH] Fix static initialization ordering bug that caused crashes at
 startup when compiling on Mac with static linking.

---
 src/google/protobuf/descriptor.cc | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc
index 48db0cc..4485fd4 100644
--- a/src/google/protobuf/descriptor.cc
+++ b/src/google/protobuf/descriptor.cc
@@ -2065,7 +2065,15 @@ const FileDescriptor* DescriptorBuilder::BuildFile(
   }
 
   result->name_ = tables_->AllocateString(proto.name());
-  result->package_ = tables_->AllocateString(proto.package());
+  if (proto.has_package()) {
+    result->package_ = tables_->AllocateString(proto.package());
+  } else {
+    // We cannot rely on proto.package() returning a valid string if
+    // proto.has_package() is false, because we might be running at static
+    // initialization time, in which case default values have not yet been
+    // initialized.
+    result->package_ = tables_->AllocateString("");
+  }
   result->pool_ = pool_;
 
   // Add to tables.
-- 
GitLab