Skip to content

Commit

Permalink
upb: fix NULL pointer bug in Python FFI
Browse files Browse the repository at this point in the history
Reference: protocolbuffers/protobuf#10208
PiperOrigin-RevId: 460623221
  • Loading branch information
ericsalo authored and copybara-github committed Jul 13, 2022
1 parent 9b3e873 commit c2c6427
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,21 @@ static PyObject* PyUpb_Message_NewStub(PyObject* parent, const upb_FieldDef* f,
return &msg->ob_base;
}

static bool PyUpb_Message_IsEmpty(const upb_Message* msg,
const upb_MessageDef* m,
const upb_DefPool* ext_pool) {
if (!msg) return true;

size_t iter = kUpb_Message_Begin;
const upb_FieldDef* f;
upb_MessageValue val;
if (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) return false;

size_t len;
(void)upb_Message_GetUnknown(msg, &len);
return len == 0;
}

static bool PyUpb_Message_IsEqual(PyUpb_Message* m1, PyObject* _m2) {
PyUpb_Message* m2 = (void*)_m2;
if (m1 == m2) return true;
Expand All @@ -569,6 +584,12 @@ static bool PyUpb_Message_IsEqual(PyUpb_Message* m1, PyObject* _m2) {
#endif
const upb_Message* m1_msg = PyUpb_Message_GetIfReified((PyObject*)m1);
const upb_Message* m2_msg = PyUpb_Message_GetIfReified(_m2);
const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m1_msgdef));

const bool e1 = PyUpb_Message_IsEmpty(m1_msg, m1_msgdef, symtab);
const bool e2 = PyUpb_Message_IsEmpty(m2_msg, m1_msgdef, symtab);
if (e1 || e2) return e1 && e2;

return upb_Message_IsEqual(m1_msg, m2_msg, m1_msgdef);
}

Expand Down

0 comments on commit c2c6427

Please sign in to comment.