From 57636ce03ac1e2aab3a362a61a6664981e21cda5 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Thu, 24 Aug 2023 14:27:41 -0700 Subject: [PATCH] upb CopyFrom the default empty message should just clear instead of deep copy from memory fix https://github.com/protocolbuffers/protobuf/issues/13485 PiperOrigin-RevId: 559870202 --- python/message.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/python/message.c b/python/message.c index afdd4311a3..e61eebb23f 100644 --- a/python/message.c +++ b/python/message.c @@ -1237,12 +1237,15 @@ static PyObject* PyUpb_Message_CopyFrom(PyObject* _self, PyObject* arg) { PyUpb_Message* other = (void*)arg; PyUpb_Message_EnsureReified(self); - PyObject* tmp = PyUpb_Message_Clear(self); - Py_DECREF(tmp); - - upb_Message_DeepCopy(self->ptr.msg, other->ptr.msg, - upb_MessageDef_MiniTable(other->def), - PyUpb_Arena_Get(self->arena)); + const upb_Message* other_msg = PyUpb_Message_GetIfReified((PyObject*)other); + if (other_msg) { + upb_Message_DeepCopy(self->ptr.msg, other_msg, + upb_MessageDef_MiniTable(other->def), + PyUpb_Arena_Get(self->arena)); + } else { + PyObject* tmp = PyUpb_Message_Clear(self); + Py_DECREF(tmp); + } PyUpb_Message_SyncSubobjs(self); Py_RETURN_NONE;