Skip to content

Conversation

@VanshAgarwal24036
Copy link
Contributor

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.

Copy link
Member

@picnixz picnixz left a 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):
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.

Comment on lines +94 to +97
if (dp->di_dbm != NULL) {
dbm_close(dp->di_dbm);
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.

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.

@bedevere-app
Copy link

bedevere-app bot commented Jan 20, 2026

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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) {
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants