[Python/upb] Fixed SEGV when attempting to delete a message attribute

Deleting an attribute is not allowed in any Proto Python implementation, but upb was not checking for this case.

PiperOrigin-RevId: 589995449
pull/15037/head
Joshua Haberman 1 year ago committed by Copybara-Service
parent 0eac77c2c6
commit de52944f38
  1. 11
      python/google/protobuf/internal/message_test.py
  2. 6
      python/message.c

@ -1340,6 +1340,17 @@ class Proto2Test(unittest.TestCase):
self.assertEqual(False, message.optional_bool)
self.assertEqual(0, message.optional_nested_message.bb)
def testDel(self):
msg = unittest_pb2.TestAllTypes()
# Fields cannot be deleted.
with self.assertRaises(AttributeError):
del msg.optional_int32
with self.assertRaises(AttributeError):
del msg.optional_bool
with self.assertRaises(AttributeError):
del msg.repeated_nested_message
def testAssignInvalidEnum(self):
"""Assigning an invalid enum number is not allowed in proto2."""
m = unittest_pb2.TestAllTypes()

@ -1005,6 +1005,12 @@ __attribute__((flatten)) static PyObject* PyUpb_Message_GetAttr(
static int PyUpb_Message_SetAttr(PyObject* _self, PyObject* attr,
PyObject* value) {
PyUpb_Message* self = (void*)_self;
if (value == NULL) {
PyErr_SetString(PyExc_AttributeError, "Cannot delete field attribute");
return -1;
}
const upb_FieldDef* field;
if (!PyUpb_Message_LookupName(self, attr, &field, NULL,
PyExc_AttributeError)) {

Loading…
Cancel
Save