-
-
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?
gh-144069: Fix memory leak in _dbm.open() on failure #144075
Conversation
picnixz
left a comment
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.
I fail to understand how this fixes the leak. Have you tried to run the reproducer case?
| self.addCleanup(cleaunup_test_dir) | ||
| setup_test_dir() | ||
|
|
||
| def test_open_nonexistent_directory(self): |
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.
| if (dp->di_dbm != NULL) { | ||
| dbm_close(dp->di_dbm); | ||
| dp->di_dbm = 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.
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.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
serhiy-storchaka
left a comment
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.
If dbm_open() may allocate internal DBM resources even if it returns NULL, we cannot do anything with this, because the returned values is the only way to access these resources.
| 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) { |
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.
Wait, we just tested that dp->di_dbm == NULL.
gh-144069
Fix a memory leak in _dbm.open() when dbm_open() fails (for example when
attempting to create a database in a non-existent directory).
dbm_open() may allocate internal DBM resources even if it returns NULL.
Ensure that any partially allocated DBM handle is properly closed on
error paths to avoid leaking memory.
Add a regression test exercising dbm.open() on a path with a missing
parent directory.