Skip to content

Commit de801be

Browse files
committed
Silence various complaints from Coverity
1 parent 96b6f8d commit de801be

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

bson/_cbsonmodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static PyObject* _error(char* name) {
9999
/* Safely downcast from Py_ssize_t to int, setting an
100100
* exception and returning -1 on error. */
101101
static int
102-
_downcast_and_check(Py_ssize_t size, int extra) {
102+
_downcast_and_check(Py_ssize_t size, uint8_t extra) {
103103
if (size > BSON_MAX_SIZE || ((BSON_MAX_SIZE - extra) < size)) {
104104
PyObject* InvalidStringData = _error("InvalidStringData");
105105
if (InvalidStringData) {
@@ -384,6 +384,7 @@ static int _load_python_objects(PyObject* module) {
384384
}
385385

386386
compiled = PyObject_CallFunction(re_compile, "O", empty_string);
387+
Py_DECREF(re_compile);
387388
if (compiled == NULL) {
388389
state->REType = NULL;
389390
Py_DECREF(empty_string);
@@ -478,10 +479,14 @@ int convert_codec_options(PyObject* options_obj, void* p) {
478479
* Return 0 on failure.
479480
*/
480481
int default_codec_options(struct module_state* state, codec_options_t* options) {
482+
PyObject* options_obj = NULL;
481483
PyObject* codec_options_func = _get_object(
482484
state->CodecOptions, "bson.codec_options", "CodecOptions");
483-
PyObject* options_obj = PyObject_CallFunctionObjArgs(
484-
codec_options_func, NULL);
485+
if (codec_options_func == NULL) {
486+
return 0;
487+
}
488+
options_obj = PyObject_CallFunctionObjArgs(codec_options_func, NULL);
489+
Py_DECREF(codec_options_func);
485490
if (options_obj == NULL) {
486491
return 0;
487492
}

pymongo/_cmessagemodule.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ static int add_last_error(PyObject* self, buffer_t buffer,
7272
int document_start;
7373
int message_length;
7474
int document_length;
75-
PyObject* key;
76-
PyObject* value;
75+
PyObject* key = NULL;
76+
PyObject* value = NULL;
7777
Py_ssize_t pos = 0;
7878
PyObject* one;
7979
char *p = strchr(ns, '.');
@@ -1290,55 +1290,65 @@ PyMODINIT_FUNC
12901290
init_cmessage(void)
12911291
#endif
12921292
{
1293-
PyObject *_cbson;
1294-
PyObject *c_api_object;
1295-
PyObject *m;
1293+
PyObject *_cbson = NULL;
1294+
PyObject *c_api_object = NULL;
1295+
PyObject *m = NULL;
12961296
struct module_state *state;
12971297

12981298
/* Store a reference to the _cbson module since it's needed to call some
12991299
* of its functions
13001300
*/
13011301
_cbson = PyImport_ImportModule("bson._cbson");
13021302
if (_cbson == NULL) {
1303-
INITERROR;
1303+
goto fail;
13041304
}
13051305

13061306
/* Import C API of _cbson
13071307
* The header file accesses _cbson_API to call the functions
13081308
*/
13091309
c_api_object = PyObject_GetAttrString(_cbson, "_C_API");
13101310
if (c_api_object == NULL) {
1311-
Py_DECREF(_cbson);
1312-
INITERROR;
1311+
goto fail;
13131312
}
13141313
#if PY_VERSION_HEX >= 0x03010000
13151314
_cbson_API = (void **)PyCapsule_GetPointer(c_api_object, "_cbson._C_API");
13161315
#else
13171316
_cbson_API = (void **)PyCObject_AsVoidPtr(c_api_object);
13181317
#endif
13191318
if (_cbson_API == NULL) {
1320-
Py_DECREF(c_api_object);
1321-
Py_DECREF(_cbson);
1322-
INITERROR;
1319+
goto fail;
13231320
}
13241321

13251322
#if PY_MAJOR_VERSION >= 3
1323+
/* Returns a new reference. */
13261324
m = PyModule_Create(&moduledef);
13271325
#else
1326+
/* Returns a borrowed reference. */
13281327
m = Py_InitModule("_cmessage", _CMessageMethods);
13291328
#endif
13301329
if (m == NULL) {
1331-
Py_DECREF(c_api_object);
1332-
Py_DECREF(_cbson);
1333-
INITERROR;
1330+
goto fail;
13341331
}
13351332

13361333
state = GETSTATE(m);
1334+
if (state == NULL) {
1335+
goto fail;
1336+
}
13371337
state->_cbson = _cbson;
13381338

13391339
Py_DECREF(c_api_object);
13401340

13411341
#if PY_MAJOR_VERSION >= 3
13421342
return m;
1343+
#else
1344+
return;
1345+
#endif
1346+
1347+
fail:
1348+
#if PY_MAJOR_VERSION >= 3
1349+
Py_XDECREF(m);
13431350
#endif
1351+
Py_XDECREF(c_api_object);
1352+
Py_XDECREF(_cbson);
1353+
INITERROR;
13441354
}

0 commit comments

Comments
 (0)