Skip to content

Commit

Permalink
[Python/upb] Fixed SEGV when attempting to delete a message attribute
Browse files Browse the repository at this point in the history
Deleting an attribute is not allowed in any Proto Python implementation, but upb was not checking for this case.

PiperOrigin-RevId: 589995449
  • Loading branch information
haberman authored and copybara-github committed Dec 12, 2023
1 parent 0eac77c commit de52944
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,17 @@ def testFieldPresence(self):
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()
Expand Down
6 changes: 6 additions & 0 deletions python/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit de52944

Please sign in to comment.