Fixed a SEGV when deep copying a non-reified sub-message.
PiperOrigin-RevId: 600951523
diff --git a/python/message.c b/python/message.c
index 2cca1c1..d394e6f 100644
--- a/python/message.c
+++ b/python/message.c
@@ -1593,10 +1593,13 @@
PyObject* DeepCopy(PyObject* _self, PyObject* arg) {
PyUpb_Message* self = (void*)_self;
const upb_MessageDef* def = PyUpb_Message_GetMsgdef(_self);
-
+ const upb_MiniTable* mini_table = upb_MessageDef_MiniTable(def);
+ upb_Message* msg = PyUpb_Message_GetIfReified(_self);
PyObject* arena = PyUpb_Arena_New();
- upb_Message* clone = upb_Message_DeepClone(
- self->ptr.msg, upb_MessageDef_MiniTable(def), PyUpb_Arena_Get(arena));
+ upb_Arena* upb_arena = PyUpb_Arena_Get(arena);
+
+ upb_Message* clone = msg ? upb_Message_DeepClone(msg, mini_table, upb_arena)
+ : upb_Message_New(mini_table, upb_arena);
PyObject* ret = PyUpb_Message_Get(clone, def, arena);
Py_DECREF(arena);