Skip to content

Commit 372b5d6

Browse files
authored
PYTHON-4305 Fix bson size check (#1564)
1 parent e68bd6f commit 372b5d6

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Diff for: bson/_cbsonmodule.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,7 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
22932293
uint32_t c_w_s_size;
22942294
uint32_t code_size;
22952295
uint32_t scope_size;
2296+
uint32_t len;
22962297
PyObject* code;
22972298
PyObject* scope;
22982299

@@ -2311,7 +2312,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
23112312
memcpy(&code_size, buffer + *position, 4);
23122313
code_size = BSON_UINT32_FROM_LE(code_size);
23132314
/* code_w_scope length + code length + code + scope length */
2314-
if (!code_size || max < code_size || max < 4 + 4 + code_size + 4) {
2315+
len = 4 + 4 + code_size + 4;
2316+
if (!code_size || max < code_size || max < len || len < code_size) {
23152317
goto invalid;
23162318
}
23172319
*position += 4;
@@ -2329,12 +2331,9 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
23292331

23302332
memcpy(&scope_size, buffer + *position, 4);
23312333
scope_size = BSON_UINT32_FROM_LE(scope_size);
2332-
if (scope_size < BSON_MIN_SIZE) {
2333-
Py_DECREF(code);
2334-
goto invalid;
2335-
}
23362334
/* code length + code + scope length + scope */
2337-
if ((4 + code_size + 4 + scope_size) != c_w_s_size) {
2335+
len = 4 + 4 + code_size + scope_size;
2336+
if (scope_size < BSON_MIN_SIZE || len != c_w_s_size || len < scope_size) {
23382337
Py_DECREF(code);
23392338
goto invalid;
23402339
}

0 commit comments

Comments
 (0)