diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index 15a37cb0a..2eb9e992d 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -2342,7 +2342,7 @@ static int _element_to_dict(PyObject* self, const char* string, if (name_length > BSON_MAX_SIZE || position + name_length >= max) { PyObject* InvalidBSON = _error("InvalidBSON"); if (InvalidBSON) { - PyErr_SetNone(InvalidBSON); + PyErr_SetString(InvalidBSON, "field name too large"); Py_DECREF(InvalidBSON); } return -1; diff --git a/test/test_bson.py b/test/test_bson.py index 7c14c625c..ccae6fcd6 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -367,6 +367,13 @@ class TestBSON(unittest.TestCase): with self.assertRaises(InvalidBSON, msg=msg): list(decode_file_iter(scratch)) + def test_invalid_field_name(self): + # Decode a truncated field + with self.assertRaises(InvalidBSON) as ctx: + decode(b'\x0b\x00\x00\x00\x02field\x00') + # Assert that the InvalidBSON error message is not empty. + self.assertTrue(str(ctx.exception)) + def test_data_timestamp(self): self.assertEqual({"test": Timestamp(4, 20)}, decode(b"\x13\x00\x00\x00\x11\x74\x65\x73\x74\x00\x14"