Skip to content

Commit 9e01a6b

Browse files
committed
PYTHON-2631 Add missing error message to InvalidBSON error (#589)
(cherry picked from commit cc029a1)
1 parent febcc51 commit 9e01a6b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

bson/_cbsonmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@ static int _element_to_dict(PyObject* self, const char* string,
26212621
if (name_length > BSON_MAX_SIZE || position + name_length >= max) {
26222622
PyObject* InvalidBSON = _error("InvalidBSON");
26232623
if (InvalidBSON) {
2624-
PyErr_SetNone(InvalidBSON);
2624+
PyErr_SetString(InvalidBSON, "field name too large");
26252625
Py_DECREF(InvalidBSON);
26262626
}
26272627
return -1;

test/test_bson.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ def test_invalid_decodes(self):
373373
with self.assertRaises(InvalidBSON, msg=msg):
374374
list(decode_file_iter(scratch))
375375

376+
def test_invalid_field_name(self):
377+
# Decode a truncated field
378+
with self.assertRaises(InvalidBSON) as ctx:
379+
decode(b'\x0b\x00\x00\x00\x02field\x00')
380+
# Assert that the InvalidBSON error message is not empty.
381+
self.assertTrue(str(ctx.exception))
382+
376383
def test_data_timestamp(self):
377384
self.assertEqual({"test": Timestamp(4, 20)},
378385
decode(b"\x13\x00\x00\x00\x11\x74\x65\x73\x74\x00\x14"

0 commit comments

Comments
 (0)