From 47c3ff405b8e850fe02e5d9d9e84741bb6a324cc Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Thu, 1 Apr 2021 15:28:15 -0700 Subject: [PATCH 1/2] PYTHON-2631 Add missing error message to InvalidBSON error --- bson/_cbsonmodule.c | 2 +- test/test_bson.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index 400a51b4e3..ac0f31243e 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, "invalid field name"); Py_DECREF(InvalidBSON); } return -1; diff --git a/test/test_bson.py b/test/test_bson.py index 7c14c625ce..ccae6fcd66 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -367,6 +367,13 @@ def test_invalid_decodes(self): 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" From 6a51c08cd4c365cbbb8c9aa89b4438a8e690940f Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Fri, 2 Apr 2021 10:05:24 -0700 Subject: [PATCH 2/2] Describe why the field was invalid --- bson/_cbsonmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index ac0f31243e..0e0f47e10b 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_SetString(InvalidBSON, "invalid field name"); + PyErr_SetString(InvalidBSON, "field name too large"); Py_DECREF(InvalidBSON); } return -1;