Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions bson/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -1174,15 +1177,15 @@ 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;
}
}
if (!buffer_write_int32(buffer, size)) {
return 0;
}
if (subtype != 2) {
if (!buffer_write_bytes(buffer, &subtype, 1)) {
if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) {
return 0;
}
}
Expand Down Expand Up @@ -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);
Comment thread
NoahStapp marked this conversation as resolved.
Resolved
flags = 0;
for (i = 0; i < flags_length; i++) {
if (buffer[*position + i] == 'i') {
Expand Down
8 changes: 7 additions & 1 deletion bson/decimal128.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
_EXPONENT_MAX = 6144
_EXPONENT_MIN = -6143
_MAX_DIGITS = 34
_MAX_COEFFICIENT = 10**_MAX_DIGITS - 1

_INF = 0x7800000000000000
_NAN = 0x7C00000000000000
Expand Down Expand Up @@ -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))
Expand Down
Binary file added fuzz/regression/decode__binary_subtype_255
Binary file not shown.
Binary file added fuzz/regression/decode__decimal128_noncanonical
Binary file not shown.
Binary file added fuzz/regression/decode__regex_flags_invalid_utf8
Binary file not shown.
14 changes: 14 additions & 0 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions test/test_decimal128.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading