Skip to content

Commit 56b6b6d

Browse files
ShaneHarveyblink1073
authored andcommitted
PYTHON-4305 Fix bson size check (#1564)
(cherry picked from commit 372b5d6)
1 parent 449d0f3 commit 56b6b6d

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

bson/_cbsonmodule.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,7 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
24052405
uint32_t c_w_s_size;
24062406
uint32_t code_size;
24072407
uint32_t scope_size;
2408+
uint32_t len;
24082409
PyObject* code;
24092410
PyObject* scope;
24102411
PyObject* code_type;
@@ -2424,7 +2425,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
24242425
memcpy(&code_size, buffer + *position, 4);
24252426
code_size = BSON_UINT32_FROM_LE(code_size);
24262427
/* code_w_scope length + code length + code + scope length */
2427-
if (!code_size || max < code_size || max < 4 + 4 + code_size + 4) {
2428+
len = 4 + 4 + code_size + 4;
2429+
if (!code_size || max < code_size || max < len || len < code_size) {
24282430
goto invalid;
24292431
}
24302432
*position += 4;
@@ -2442,12 +2444,9 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
24422444

24432445
memcpy(&scope_size, buffer + *position, 4);
24442446
scope_size = BSON_UINT32_FROM_LE(scope_size);
2445-
if (scope_size < BSON_MIN_SIZE) {
2446-
Py_DECREF(code);
2447-
goto invalid;
2448-
}
24492447
/* code length + code + scope length + scope */
2450-
if ((4 + code_size + 4 + scope_size) != c_w_s_size) {
2448+
len = 4 + 4 + code_size + scope_size;
2449+
if (scope_size < BSON_MIN_SIZE || len != c_w_s_size || len < scope_size) {
24512450
Py_DECREF(code);
24522451
goto invalid;
24532452
}

0 commit comments

Comments
 (0)