Kaydet (Commit) ef1701f7 authored tarafından Georg Brandl's avatar Georg Brandl

Add additional missing checks for return vals of PyTuple_New().

Normalize coding style.
üst 89ba3815
...@@ -1986,14 +1986,14 @@ DB_set_bt_minkey(DBObject* self, PyObject* args) ...@@ -1986,14 +1986,14 @@ DB_set_bt_minkey(DBObject* self, PyObject* args)
#if (DBVER >= 33) #if (DBVER >= 33)
static int static int
_default_cmp (const DBT *leftKey, _default_cmp(const DBT *leftKey,
const DBT *rightKey) const DBT *rightKey)
{ {
int res; int res;
int lsize = leftKey->size, rsize = rightKey->size; int lsize = leftKey->size, rsize = rightKey->size;
res = memcmp (leftKey->data, rightKey->data, res = memcmp(leftKey->data, rightKey->data,
lsize < rsize ? lsize : rsize); lsize < rsize ? lsize : rsize);
if (res == 0) { if (res == 0) {
if (lsize < rsize) { if (lsize < rsize) {
...@@ -2007,58 +2007,56 @@ _default_cmp (const DBT *leftKey, ...@@ -2007,58 +2007,56 @@ _default_cmp (const DBT *leftKey,
} }
static int static int
_db_compareCallback (DB* db, _db_compareCallback(DB* db,
const DBT *leftKey, const DBT *leftKey,
const DBT *rightKey) const DBT *rightKey)
{ {
int res = 0; int res = 0;
PyObject *args; PyObject *args;
PyObject *result; PyObject *result;
PyObject *leftObject; PyObject *leftObject;
PyObject *rightObject; PyObject *rightObject;
DBObject *self = (DBObject *) db->app_private; DBObject *self = (DBObject *)db->app_private;
if (self == NULL || self->btCompareCallback == NULL) { if (self == NULL || self->btCompareCallback == NULL) {
MYDB_BEGIN_BLOCK_THREADS; MYDB_BEGIN_BLOCK_THREADS;
PyErr_SetString (PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
(self == 0 (self == 0
? "DB_bt_compare db is NULL." ? "DB_bt_compare db is NULL."
: "DB_bt_compare callback is NULL.")); : "DB_bt_compare callback is NULL."));
/* we're in a callback within the DB code, we can't raise */ /* we're in a callback within the DB code, we can't raise */
PyErr_Print (); PyErr_Print();
res = _default_cmp (leftKey, rightKey); res = _default_cmp(leftKey, rightKey);
MYDB_END_BLOCK_THREADS; MYDB_END_BLOCK_THREADS;
} } else {
else {
MYDB_BEGIN_BLOCK_THREADS; MYDB_BEGIN_BLOCK_THREADS;
leftObject = PyString_FromStringAndSize (leftKey->data, leftKey->size); leftObject = PyString_FromStringAndSize(leftKey->data, leftKey->size);
rightObject = PyString_FromStringAndSize (rightKey->data, rightKey->size); rightObject = PyString_FromStringAndSize(rightKey->data, rightKey->size);
args = PyTuple_New (2); args = PyTuple_New(2);
Py_INCREF (self); if (args != NULL) {
PyTuple_SET_ITEM (args, 0, leftObject); /* steals reference */ Py_INCREF(self);
PyTuple_SET_ITEM (args, 1, rightObject); /* steals reference */ PyTuple_SET_ITEM(args, 0, leftObject); /* steals reference */
PyTuple_SET_ITEM(args, 1, rightObject); /* steals reference */
result = PyEval_CallObject (self->btCompareCallback, args); result = PyEval_CallObject(self->btCompareCallback, args);
if (result == 0) {
/* we're in a callback within the DB code, we can't raise */
PyErr_Print ();
res = _default_cmp (leftKey, rightKey);
}
else if (PyInt_Check (result)) {
res = PyInt_AsLong (result);
} }
else { if (args == NULL || result == NULL) {
PyErr_SetString (PyExc_TypeError, /* we're in a callback within the DB code, we can't raise */
"DB_bt_compare callback MUST return an int."); PyErr_Print();
res = _default_cmp(leftKey, rightKey);
} else if (PyInt_Check(result)) {
res = PyInt_AsLong(result);
} else {
PyErr_SetString(PyExc_TypeError,
"DB_bt_compare callback MUST return an int.");
/* we're in a callback within the DB code, we can't raise */ /* we're in a callback within the DB code, we can't raise */
PyErr_Print (); PyErr_Print();
res = _default_cmp (leftKey, rightKey); res = _default_cmp(leftKey, rightKey);
} }
Py_DECREF (args); Py_DECREF(args);
Py_XDECREF (result); Py_XDECREF(result);
MYDB_END_BLOCK_THREADS; MYDB_END_BLOCK_THREADS;
} }
...@@ -2066,19 +2064,19 @@ _db_compareCallback (DB* db, ...@@ -2066,19 +2064,19 @@ _db_compareCallback (DB* db,
} }
static PyObject* static PyObject*
DB_set_bt_compare (DBObject* self, PyObject* args) DB_set_bt_compare(DBObject* self, PyObject* args)
{ {
int err; int err;
PyObject *comparator; PyObject *comparator;
PyObject *tuple, *emptyStr, *result; PyObject *tuple, *emptyStr, *result;
if (!PyArg_ParseTuple(args,"O:set_bt_compare", &comparator )) if (!PyArg_ParseTuple(args, "O:set_bt_compare", &comparator))
return NULL; return NULL;
CHECK_DB_NOT_CLOSED (self); CHECK_DB_NOT_CLOSED(self);
if (! PyCallable_Check (comparator)) { if (!PyCallable_Check(comparator)) {
makeTypeError ("Callable", comparator); makeTypeError("Callable", comparator);
return NULL; return NULL;
} }
...@@ -2087,22 +2085,23 @@ DB_set_bt_compare (DBObject* self, PyObject* args) ...@@ -2087,22 +2085,23 @@ DB_set_bt_compare (DBObject* self, PyObject* args)
* string objects here. verify that it returns an int (0). * string objects here. verify that it returns an int (0).
* err if not. * err if not.
*/ */
tuple = PyTuple_New (2); tuple = PyTuple_New(2);
emptyStr = PyString_FromStringAndSize(NULL, 0);
emptyStr = PyString_FromStringAndSize (NULL, 0); if (tuple == NULL || emptyStr == NULL)
Py_INCREF(emptyStr); return NULL;
PyTuple_SET_ITEM (tuple, 0, emptyStr);
PyTuple_SET_ITEM (tuple, 1, emptyStr); /* steals reference */ Py_INCREF(emptyStr); /* now we have two references */
result = PyEval_CallObject (comparator, tuple); PyTuple_SET_ITEM(tuple, 0, emptyStr); /* steals reference */
Py_DECREF (tuple); PyTuple_SET_ITEM(tuple, 1, emptyStr); /* steals reference */
if (result == 0 || !PyInt_Check(result)) { result = PyEval_CallObject(comparator, tuple);
PyErr_SetString (PyExc_TypeError, Py_DECREF(tuple);
"callback MUST return an int"); if (result == NULL || !PyInt_Check(result)) {
PyErr_SetString(PyExc_TypeError,
"callback MUST return an int");
return NULL; return NULL;
} } else if (PyInt_AsLong(result) != 0) {
else if (PyInt_AsLong(result) != 0) { PyErr_SetString(PyExc_TypeError,
PyErr_SetString (PyExc_TypeError, "callback failed to return 0 on two empty strings");
"callback failed to return 0 on two empty strings");
return NULL; return NULL;
} }
...@@ -2110,11 +2109,11 @@ DB_set_bt_compare (DBObject* self, PyObject* args) ...@@ -2110,11 +2109,11 @@ DB_set_bt_compare (DBObject* self, PyObject* args)
* simplify the code. This would have no real use, as one cannot * simplify the code. This would have no real use, as one cannot
* change the function once the db is opened anyway */ * change the function once the db is opened anyway */
if (self->btCompareCallback != NULL) { if (self->btCompareCallback != NULL) {
PyErr_SetString (PyExc_RuntimeError, "set_bt_compare () cannot be called more than once"); PyErr_SetString(PyExc_RuntimeError, "set_bt_compare() cannot be called more than once");
return NULL; return NULL;
} }
Py_INCREF (comparator); Py_INCREF(comparator);
self->btCompareCallback = comparator; self->btCompareCallback = comparator;
/* This is to workaround a problem with un-initialized threads (see /* This is to workaround a problem with un-initialized threads (see
...@@ -2123,18 +2122,18 @@ DB_set_bt_compare (DBObject* self, PyObject* args) ...@@ -2123,18 +2122,18 @@ DB_set_bt_compare (DBObject* self, PyObject* args)
PyEval_InitThreads(); PyEval_InitThreads();
#endif #endif
err = self->db->set_bt_compare (self->db, err = self->db->set_bt_compare(self->db,
(comparator != NULL ? (comparator != NULL ?
_db_compareCallback : NULL)); _db_compareCallback : NULL));
if (err) { if (err) {
/* restore the old state in case of error */ /* restore the old state in case of error */
Py_DECREF (comparator); Py_DECREF(comparator);
self->btCompareCallback = NULL; self->btCompareCallback = NULL;
} }
RETURN_IF_ERR (); RETURN_IF_ERR();
RETURN_NONE (); RETURN_NONE();
} }
#endif /* DBVER >= 33 */ #endif /* DBVER >= 33 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment