@@ -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
12901290init_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