python-pymongo/0001-PYTHON-2631-Add-missing-error-message-to-InvalidBSON.patch
liubo 266b0e8122 Add missing error message to InvalidBSON error
Signed-off-by: liubo <liubo1@xfusion.com>
(cherry picked from commit a83c461dba8c95d02f5ae7028f57e8eeac9b09f1)
2024-04-07 11:18:37 +08:00

47 lines
1.7 KiB
Diff

From 9e01a6bf1d081c62ac2173f666234de159397e0c Mon Sep 17 00:00:00 2001
From: Shane Harvey <shane.harvey@mongodb.com>
Date: Fri, 2 Apr 2021 10:17:04 -0700
Subject: [PATCH] PYTHON-2631 Add missing error message to InvalidBSON error
(#589)
(cherry picked from commit cc029a1e6208863eaab453777363d3935b927f32)
---
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 34f3ab6f..1522bb0a 100644
--- a/bson/_cbsonmodule.c
+++ b/bson/_cbsonmodule.c
@@ -2621,7 +2621,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 ad726f71..f75da3d2 100644
--- a/test/test_bson.py
+++ b/test/test_bson.py
@@ -373,6 +373,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"
--
2.42.0.windows.2