title: Setting a Protobuf Submessage or Embeded Message and return the Message Object
body: <p>I run into a Segfault when using protocol-buffers with submessages. I can't post all my code so I try to give an example.</p>
  
  <p>The .proto code is something like this:</p>
  
  <pre><code>Message {
      optional int id = 1;
      optional SubMessage submsg = 2;
  }
  
  SubMessage {
      optional int foo = 1;
  }
  </code></pre>
  
  <p>In the C++ code I have a function which creates and returns a SubMessage to an other function which embeds the SubMessage into a Message and returns that one to an other function,...</p>
  
  <pre><code>SubMessage createSubMessage() {
      SubMessage smsg;
      smsg.set_foo(12);
      return smsg;
  }
  
  Message createMessage() {
      Message msg;
      // with this line i get the segfault, without not
      SubMessage* smsg = msg.mutable_submsg();
      // I get the segfault no matter if i actually
      // set the submessage or not. 
      // smsg.CopyFrom(createSubMessage();
      return msg;
  }
  
  void foo() {
      for(;;) {
          Message msg;
          msg = createMessage();
     }
  }
  </code></pre>
  
  <p>And the error I am getting is coming from SubMessage::MergeFrom()</p>
  
  <pre><code>[libprotobuf FATAL messges_pb.cc:1128] CHECK failed: (&amp;from) != (this): 
  Segmentation fault
  </code></pre>
  
  <p>The Segfault interestingly appears in the second iteration of the loop, at the second assignment to msg.</p>
  
  <p>I already tried a lot of fuzzing, allocating a SubMessage on the heap and setting it with set_allocated_submsg(), CopyFrom(), MergeFrom(), copy constructors and so on,... I am still running into the same problem.</p>