Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef1701f

Browse files
committed
Add additional missing checks for return vals of PyTuple_New().
Normalize coding style.
1 parent 89ba381 commit ef1701f

1 file changed

Lines changed: 65 additions & 66 deletions

File tree

Modules/_bsddb.c

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,14 +1986,14 @@ DB_set_bt_minkey(DBObject* self, PyObject* args)
19861986

19871987
#if (DBVER >= 33)
19881988
static int
1989-
_default_cmp (const DBT *leftKey,
1990-
const DBT *rightKey)
1989+
_default_cmp(const DBT *leftKey,
1990+
const DBT *rightKey)
19911991
{
19921992
int res;
19931993
int lsize = leftKey->size, rsize = rightKey->size;
19941994

1995-
res = memcmp (leftKey->data, rightKey->data,
1996-
lsize < rsize ? lsize : rsize);
1995+
res = memcmp(leftKey->data, rightKey->data,
1996+
lsize < rsize ? lsize : rsize);
19971997

19981998
if (res == 0) {
19991999
if (lsize < rsize) {
@@ -2007,78 +2007,76 @@ _default_cmp (const DBT *leftKey,
20072007
}
20082008

20092009
static int
2010-
_db_compareCallback (DB* db,
2011-
const DBT *leftKey,
2012-
const DBT *rightKey)
2010+
_db_compareCallback(DB* db,
2011+
const DBT *leftKey,
2012+
const DBT *rightKey)
20132013
{
20142014
int res = 0;
20152015
PyObject *args;
20162016
PyObject *result;
20172017
PyObject *leftObject;
20182018
PyObject *rightObject;
2019-
DBObject *self = (DBObject *) db->app_private;
2019+
DBObject *self = (DBObject *)db->app_private;
20202020

20212021
if (self == NULL || self->btCompareCallback == NULL) {
20222022
MYDB_BEGIN_BLOCK_THREADS;
2023-
PyErr_SetString (PyExc_TypeError,
2024-
(self == 0
2025-
? "DB_bt_compare db is NULL."
2026-
: "DB_bt_compare callback is NULL."));
2023+
PyErr_SetString(PyExc_TypeError,
2024+
(self == 0
2025+
? "DB_bt_compare db is NULL."
2026+
: "DB_bt_compare callback is NULL."));
20272027
/* we're in a callback within the DB code, we can't raise */
2028-
PyErr_Print ();
2029-
res = _default_cmp (leftKey, rightKey);
2028+
PyErr_Print();
2029+
res = _default_cmp(leftKey, rightKey);
20302030
MYDB_END_BLOCK_THREADS;
2031-
}
2032-
else {
2031+
} else {
20332032
MYDB_BEGIN_BLOCK_THREADS;
20342033

2035-
leftObject = PyString_FromStringAndSize (leftKey->data, leftKey->size);
2036-
rightObject = PyString_FromStringAndSize (rightKey->data, rightKey->size);
2034+
leftObject = PyString_FromStringAndSize(leftKey->data, leftKey->size);
2035+
rightObject = PyString_FromStringAndSize(rightKey->data, rightKey->size);
20372036

2038-
args = PyTuple_New (2);
2039-
Py_INCREF (self);
2040-
PyTuple_SET_ITEM (args, 0, leftObject); /* steals reference */
2041-
PyTuple_SET_ITEM (args, 1, rightObject); /* steals reference */
2042-
2043-
result = PyEval_CallObject (self->btCompareCallback, args);
2044-
if (result == 0) {
2045-
/* we're in a callback within the DB code, we can't raise */
2046-
PyErr_Print ();
2047-
res = _default_cmp (leftKey, rightKey);
2048-
}
2049-
else if (PyInt_Check (result)) {
2050-
res = PyInt_AsLong (result);
2037+
args = PyTuple_New(2);
2038+
if (args != NULL) {
2039+
Py_INCREF(self);
2040+
PyTuple_SET_ITEM(args, 0, leftObject); /* steals reference */
2041+
PyTuple_SET_ITEM(args, 1, rightObject); /* steals reference */
2042+
result = PyEval_CallObject(self->btCompareCallback, args);
20512043
}
2052-
else {
2053-
PyErr_SetString (PyExc_TypeError,
2054-
"DB_bt_compare callback MUST return an int.");
2044+
if (args == NULL || result == NULL) {
2045+
/* we're in a callback within the DB code, we can't raise */
2046+
PyErr_Print();
2047+
res = _default_cmp(leftKey, rightKey);
2048+
} else if (PyInt_Check(result)) {
2049+
res = PyInt_AsLong(result);
2050+
} else {
2051+
PyErr_SetString(PyExc_TypeError,
2052+
"DB_bt_compare callback MUST return an int.");
20552053
/* we're in a callback within the DB code, we can't raise */
2056-
PyErr_Print ();
2057-
res = _default_cmp (leftKey, rightKey);
2054+
PyErr_Print();
2055+
res = _default_cmp(leftKey, rightKey);
20582056
}
20592057

2060-
Py_DECREF (args);
2061-
Py_XDECREF (result);
2058+
Py_DECREF(args);
2059+
Py_XDECREF(result);
20622060

20632061
MYDB_END_BLOCK_THREADS;
20642062
}
20652063
return res;
20662064
}
20672065

20682066
static PyObject*
2069-
DB_set_bt_compare (DBObject* self, PyObject* args)
2067+
DB_set_bt_compare(DBObject* self, PyObject* args)
20702068
{
20712069
int err;
20722070
PyObject *comparator;
20732071
PyObject *tuple, *emptyStr, *result;
20742072

2075-
if (!PyArg_ParseTuple(args,"O:set_bt_compare", &comparator ))
2073+
if (!PyArg_ParseTuple(args, "O:set_bt_compare", &comparator))
20762074
return NULL;
20772075

2078-
CHECK_DB_NOT_CLOSED (self);
2076+
CHECK_DB_NOT_CLOSED(self);
20792077

2080-
if (! PyCallable_Check (comparator)) {
2081-
makeTypeError ("Callable", comparator);
2078+
if (!PyCallable_Check(comparator)) {
2079+
makeTypeError("Callable", comparator);
20822080
return NULL;
20832081
}
20842082

@@ -2087,34 +2085,35 @@ DB_set_bt_compare (DBObject* self, PyObject* args)
20872085
* string objects here. verify that it returns an int (0).
20882086
* err if not.
20892087
*/
2090-
tuple = PyTuple_New (2);
2091-
2092-
emptyStr = PyString_FromStringAndSize (NULL, 0);
2093-
Py_INCREF(emptyStr);
2094-
PyTuple_SET_ITEM (tuple, 0, emptyStr);
2095-
PyTuple_SET_ITEM (tuple, 1, emptyStr); /* steals reference */
2096-
result = PyEval_CallObject (comparator, tuple);
2097-
Py_DECREF (tuple);
2098-
if (result == 0 || !PyInt_Check(result)) {
2099-
PyErr_SetString (PyExc_TypeError,
2100-
"callback MUST return an int");
2088+
tuple = PyTuple_New(2);
2089+
emptyStr = PyString_FromStringAndSize(NULL, 0);
2090+
if (tuple == NULL || emptyStr == NULL)
2091+
return NULL;
2092+
2093+
Py_INCREF(emptyStr); /* now we have two references */
2094+
PyTuple_SET_ITEM(tuple, 0, emptyStr); /* steals reference */
2095+
PyTuple_SET_ITEM(tuple, 1, emptyStr); /* steals reference */
2096+
result = PyEval_CallObject(comparator, tuple);
2097+
Py_DECREF(tuple);
2098+
if (result == NULL || !PyInt_Check(result)) {
2099+
PyErr_SetString(PyExc_TypeError,
2100+
"callback MUST return an int");
21012101
return NULL;
2102-
}
2103-
else if (PyInt_AsLong(result) != 0) {
2104-
PyErr_SetString (PyExc_TypeError,
2105-
"callback failed to return 0 on two empty strings");
2102+
} else if (PyInt_AsLong(result) != 0) {
2103+
PyErr_SetString(PyExc_TypeError,
2104+
"callback failed to return 0 on two empty strings");
21062105
return NULL;
21072106
}
21082107

21092108
/* We don't accept multiple set_bt_compare operations, in order to
21102109
* simplify the code. This would have no real use, as one cannot
21112110
* change the function once the db is opened anyway */
21122111
if (self->btCompareCallback != NULL) {
2113-
PyErr_SetString (PyExc_RuntimeError, "set_bt_compare () cannot be called more than once");
2112+
PyErr_SetString(PyExc_RuntimeError, "set_bt_compare() cannot be called more than once");
21142113
return NULL;
21152114
}
21162115

2117-
Py_INCREF (comparator);
2116+
Py_INCREF(comparator);
21182117
self->btCompareCallback = comparator;
21192118

21202119
/* This is to workaround a problem with un-initialized threads (see
@@ -2123,18 +2122,18 @@ DB_set_bt_compare (DBObject* self, PyObject* args)
21232122
PyEval_InitThreads();
21242123
#endif
21252124

2126-
err = self->db->set_bt_compare (self->db,
2127-
(comparator != NULL ?
2128-
_db_compareCallback : NULL));
2125+
err = self->db->set_bt_compare(self->db,
2126+
(comparator != NULL ?
2127+
_db_compareCallback : NULL));
21292128

21302129
if (err) {
21312130
/* restore the old state in case of error */
2132-
Py_DECREF (comparator);
2131+
Py_DECREF(comparator);
21332132
self->btCompareCallback = NULL;
21342133
}
21352134

2136-
RETURN_IF_ERR ();
2137-
RETURN_NONE ();
2135+
RETURN_IF_ERR();
2136+
RETURN_NONE();
21382137
}
21392138
#endif /* DBVER >= 33 */
21402139

0 commit comments

Comments
 (0)