Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_dbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_error(self):
def test_anydbm_not_existing(self):
self.assertRaises(dbm.error, dbm.open, _fname)

def test_open_nonexistent_directory(self):
missing_dir = os.path.join(dirname + "_does_not_exist", "test.db")
with self.assertRaises(OSError):
dbm.open(missing_dir, "c")

def test_anydbm_creation(self):
f = dbm.open(_fname, 'c')
self.assertEqual(list(f.keys()), [])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a memory leak in :func:`dbm.open` when database creation fails, such as
when the target directory does not exist.
4 changes: 3 additions & 1 deletion Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ newdbmobject(_dbm_state *state, const char *file, int flags, int mode)
}
dp->di_size = -1;
dp->flags = flags;
dp->di_dbm = NULL;
PyObject_GC_Track(dp);

/* See issue #19296 */
if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == NULL ) {
PyErr_SetFromErrnoWithFilename(state->dbm_error, file);
Py_DECREF(dp);
return NULL;
Expand All @@ -105,6 +106,7 @@ dbm_dealloc(PyObject *self)
PyObject_GC_UnTrack(dp);
if (dp->di_dbm) {
dbm_close(dp->di_dbm);
dp->di_dbm = NULL;
}
PyTypeObject *tp = Py_TYPE(dp);
tp->tp_free(dp);
Expand Down
Loading