-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-144069: Fix memory leak in _dbm.open() on failure #144075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, we just tested that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification. 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.