Skip to content

Commit de52944

Browse files
habermancopybara-github
authored andcommitted
[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
1 parent 0eac77c commit de52944

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

python/google/protobuf/internal/message_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,17 @@ def testFieldPresence(self):
13401340
self.assertEqual(False, message.optional_bool)
13411341
self.assertEqual(0, message.optional_nested_message.bb)
13421342

1343+
def testDel(self):
1344+
msg = unittest_pb2.TestAllTypes()
1345+
1346+
# Fields cannot be deleted.
1347+
with self.assertRaises(AttributeError):
1348+
del msg.optional_int32
1349+
with self.assertRaises(AttributeError):
1350+
del msg.optional_bool
1351+
with self.assertRaises(AttributeError):
1352+
del msg.repeated_nested_message
1353+
13431354
def testAssignInvalidEnum(self):
13441355
"""Assigning an invalid enum number is not allowed in proto2."""
13451356
m = unittest_pb2.TestAllTypes()

python/message.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,12 @@ __attribute__((flatten)) static PyObject* PyUpb_Message_GetAttr(
10051005
static int PyUpb_Message_SetAttr(PyObject* _self, PyObject* attr,
10061006
PyObject* value) {
10071007
PyUpb_Message* self = (void*)_self;
1008+
1009+
if (value == NULL) {
1010+
PyErr_SetString(PyExc_AttributeError, "Cannot delete field attribute");
1011+
return -1;
1012+
}
1013+
10081014
const upb_FieldDef* field;
10091015
if (!PyUpb_Message_LookupName(self, attr, &field, NULL,
10101016
PyExc_AttributeError)) {

0 commit comments

Comments
 (0)