diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index 7f221f72c9..a0deb7de77 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -1146,7 +1146,8 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, { /* Binary */ PyObject* subtype_object; - char subtype; + long subtype_long; + unsigned char subtype; const char* data; int size; @@ -1155,11 +1156,13 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, if (!subtype_object) { return 0; } - subtype = (char)PyLong_AsLong(subtype_object); - if (subtype == -1) { + subtype_long = PyLong_AsLong(subtype_object); + if (subtype_long == -1 && PyErr_Occurred()) { Py_DECREF(subtype_object); return 0; } + /* Subtype is 0-255, so a signed char would turn 255 into -1. */ + subtype = (unsigned char)subtype_long; size = _downcast_and_check(PyBytes_Size(value), 0); if (size == -1) { Py_DECREF(subtype_object); @@ -1174,7 +1177,7 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, if (!buffer_write_int32(buffer, other_size)) { return 0; } - if (!buffer_write_bytes(buffer, &subtype, 1)) { + if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) { return 0; } } @@ -1182,7 +1185,7 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, return 0; } if (subtype != 2) { - if (!buffer_write_bytes(buffer, &subtype, 1)) { + if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) { return 0; } } @@ -2496,6 +2499,15 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer, Py_DECREF(pattern); goto invalid; } + /* Validate the flags as UTF-8. */ + PyObject* flags_str = PyUnicode_DecodeUTF8( + start, flags_length, + options->unicode_decode_error_handler); + if (!flags_str) { + Py_DECREF(pattern); + goto invalid; + } + Py_DECREF(flags_str); flags = 0; for (i = 0; i < flags_length; i++) { if (buffer[*position + i] == 'i') { diff --git a/bson/decimal128.py b/bson/decimal128.py index efdbecb3fc..b7304fc3f0 100644 --- a/bson/decimal128.py +++ b/bson/decimal128.py @@ -35,6 +35,7 @@ _EXPONENT_MAX = 6144 _EXPONENT_MIN = -6143 _MAX_DIGITS = 34 +_MAX_COEFFICIENT = 10**_MAX_DIGITS - 1 _INF = 0x7800000000000000 _NAN = 0x7C00000000000000 @@ -303,8 +304,13 @@ def to_decimal(self) -> decimal.Decimal: mask = 0x0001000000000000 arr[0] = (high & mask) >> 48 + coefficient = int.from_bytes(arr, "big") + if coefficient > _MAX_COEFFICIENT: + # A significand of more than 34 digits is treated as 0. + return decimal.Decimal((sign, (0,), exponent)) + # cdecimal only accepts a tuple for digits. - digits = tuple(int(digit) for digit in str(int.from_bytes(arr, "big"))) + digits = tuple(int(digit) for digit in str(coefficient)) with decimal.localcontext(_DEC128_CTX) as ctx: return ctx.create_decimal((sign, digits, exponent)) diff --git a/fuzz/regression/decode__binary_subtype_255 b/fuzz/regression/decode__binary_subtype_255 new file mode 100644 index 0000000000..6ad8db1391 Binary files /dev/null and b/fuzz/regression/decode__binary_subtype_255 differ diff --git a/fuzz/regression/decode__decimal128_noncanonical b/fuzz/regression/decode__decimal128_noncanonical new file mode 100644 index 0000000000..3b866abf1f Binary files /dev/null and b/fuzz/regression/decode__decimal128_noncanonical differ diff --git a/fuzz/regression/decode__regex_flags_invalid_utf8 b/fuzz/regression/decode__regex_flags_invalid_utf8 new file mode 100644 index 0000000000..22ffe80b87 Binary files /dev/null and b/fuzz/regression/decode__regex_flags_invalid_utf8 differ diff --git a/test/test_bson.py b/test/test_bson.py index d07a9b4bb6..d3940bda7f 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -463,6 +463,20 @@ def test_regex_empty_flags(self): self.assertEqual(decode(mm), expected) self.assertEqual(decode_all(mm), [expected]) + def test_binary_subtype_255_round_trip(self): + for subtype in (0x80, 0xFE, 0xFF): + with self.subTest(subtype=subtype): + doc = {"x": Binary(b"a" * 16, subtype)} + self.assertEqual(decode(encode(doc)), doc) + + def test_regex_invalid_utf8_flags(self): + data = b"\x0f\x00\x00\x00\x0ba\x00abc\x00\xc4m\x00\x00" + self.assertFalse(is_valid(data)) + with self.assertRaises(InvalidBSON): + decode(data) + with self.assertRaises(InvalidBSON): + decode_all(data) + def test_element_overlaps_document_terminator(self): # PYTHON-6111: an element whose value would consume the document # terminator byte must be rejected, matching the pure-Python decoder. diff --git a/test/test_decimal128.py b/test/test_decimal128.py index 9ea20ed84f..666987dccb 100644 --- a/test/test_decimal128.py +++ b/test/test_decimal128.py @@ -68,6 +68,13 @@ def test_decimal128_context(self): self.assertEqual("Infinity", str(ctx.copy().create_decimal("1E6145"))) self.assertEqual("0E-6176", str(ctx.copy().create_decimal("1E-6177"))) + def test_significand_too_large(self): + bid = bytes.fromhex("003d203b9db5056f05000000fcffffdb") + dec = Decimal128.from_bid(bid) + self.assertEqual(Decimal(0), dec.to_decimal()) + self.assertEqual("-0E+5599", str(dec)) + self.assertEqual("Decimal128('-0E+5599')", repr(dec)) + if __name__ == "__main__": unittest.main()