-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
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
base: main
Are you sure you want to change the base?
Changes from all 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 |
||
| dbm_close(dp->di_dbm); | ||
| dp->di_dbm = NULL; | ||
| } | ||
|
Comment on lines
+94
to
+97
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. |
||
| 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.