Browse Source

Force a copy when saving the NSData that came from another.

Thomas Van Lenten 7 years ago
parent
commit
4588e6e2b9
1 changed files with 6 additions and 1 deletions
  1. 6 1
      objectivec/GPBMessage.m

+ 6 - 1
objectivec/GPBMessage.m

@@ -2027,7 +2027,12 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
       [newInput release];
     } else {
       GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
-      [unknownFields mergeMessageSetMessage:typeId data:rawBytes];
+      // rawBytes was created via a NoCopy, so it can be reusing a
+      // subrange of another NSData that might go out of scope as things
+      // unwind, so a copy is needed to ensure what is saved in the
+      // unknown fields stays valid.
+      NSData *cloned = [NSData dataWithData:rawBytes];
+      [unknownFields mergeMessageSetMessage:typeId data:cloned];
     }
   }
 }