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.

Copy link
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
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.

@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.

Copy link
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.

@picnixz
Copy link
Member

picnixz commented Jan 21, 2026

This does not fix anything. I am closing it as is as I believe it is an upstream issue. But we need to investigate differently, maybe with a C reproducer. This PR is, in its shape wrong, and does not address the concern about the original leak.

@picnixz picnixz closed this Jan 21, 2026
@VanshAgarwal24036 VanshAgarwal24036 deleted the gh-144069-fix-dbm-leak branch January 21, 2026 05:35
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