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

Skip to content

Commit b6c9f78

Browse files
committed
bugfix: do not double-close DB cursor during deallocation when the
underlying DB has already been closed (and thus all of its cursors). This fixes a potential segfault. SF pybsddb bug id 667343 bugfix: close the DB object when raising an exception due to an error during DB.open. This prevents an exception when closing the environment about not all databases being closed. SF pybsddb bug id 667340
1 parent aa71f5f commit b6c9f78

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/bsddb/test/test_basics.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ def test03_SimpleCursorStuff(self):
398398
self.fail("no exception raised when using a buggy cursor's"
399399
"%s method" % method)
400400

401+
#
402+
# free cursor referencing a closed database, it should not barf:
403+
#
404+
oldcursor = self.d.cursor(txn=txn)
405+
self.d.close()
406+
407+
# this would originally cause a segfault when the cursor for a
408+
# closed database was cleaned up. it should not anymore.
409+
# SF pybsddb bug id 667343
410+
del oldcursor
411+
412+
401413
#----------------------------------------
402414

403415
def test04_PartialGetAndPut(self):

Modules/_bsddb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ DBCursor_dealloc(DBCursorObject* self)
746746
int err;
747747
if (self->dbc != NULL) {
748748
MYDB_BEGIN_ALLOW_THREADS;
749-
err = self->dbc->c_close(self->dbc);
749+
if (self->mydb->db != NULL)
750+
err = self->dbc->c_close(self->dbc);
750751
self->dbc = NULL;
751752
MYDB_END_ALLOW_THREADS;
752753
}
@@ -1623,6 +1624,7 @@ DB_open(DBObject* self, PyObject* args, PyObject* kwargs)
16231624
#endif
16241625
MYDB_END_ALLOW_THREADS;
16251626
if (makeDBError(err)) {
1627+
self->db->close(self->db, 0);
16261628
self->db = NULL;
16271629
return NULL;
16281630
}

0 commit comments

Comments
 (0)