Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out — you’re right.

Since dbm_open() returns NULL, there is no DBM handle available to clean up, so attempting to close anything on the failure path was indeed dead code.

I’ve removed that logic from newdbmobject() and instead updated dbm_dealloc() to set dp->di_dbm = NULL after dbm_close() to make deallocation robust and avoid double frees.

dbm_close(dp->di_dbm);
dp->di_dbm = NULL;
}
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification.
You’re right, since dbm_open() returns NULL, there is no DBM handle available to clean up on the failure path so cleanup attempt in newdbmobject() was dead code.

I removed that logic and instead updated dbm_dealloc() to set dp->di_dbm = NULL after dbm_close() to prevent double frees and stale pointer use.

I’ve also moved the regression test closer to the other open()-related tests as suggested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is an LLM answer. I will reject this PR because I am tired of you using LLMs to generate PRs when we repeatedly told you not to do it. If you do not understand the issue and how to fix it in the first time or do not understand and review the LLM changes before committing, I would appreciate that you stop opening PRs.

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