Skip to content
Open
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
4 changes: 4 additions & 0 deletions Lib/test/test_dbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ def setUp(self):
self.addCleanup(cleaunup_test_dir)
setup_test_dir()

def test_open_nonexistent_directory(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the test closer to other open()-tests? please be mindful of the quality of the PRs you submit.

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

class WhichDBTestCase(unittest.TestCase):
def test_whichdb(self):
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.
7 changes: 6 additions & 1 deletion Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ 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);
if (dp->di_dbm != NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we just tested that dp->di_dbm == NULL.

dbm_close(dp->di_dbm);
dp->di_dbm = NULL;
}
Comment on lines +94 to +97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for that, you can leave it to dealloc. In addition this code path will never be taken as di_dbm will be NULL... this is exactly your if test.

However add dp->di_dbm = NULL in dbm_dealloc() to prevent double frees.

Py_DECREF(dp);
return NULL;
}
Expand Down
Loading