From de52944f389c85943f35049cc9cdf9ea0a3ca380 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 11 Dec 2023 16:55:43 -0800 Subject: [PATCH] [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 --- python/google/protobuf/internal/message_test.py | 11 +++++++++++ python/message.c | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index b0f1ae784fa6..a6223c2a7edf 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -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() diff --git a/python/message.c b/python/message.c index 2aea2c5a9ff9..2cca1c1f0f19 100644 --- a/python/message.c +++ b/python/message.c @@ -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)) {