fix(db): free handle/join-cursor state on create-time OOM error paths - #92
Merged
Conversation
…aths Two memory leaks on out-of-memory paths, found by the extended malloc-fault injection sweep (PR #90): - __db_create_internal (db_method.c): the err: path freed dbp and dbp->mpf but not the access-method-specific handle state (bt_internal / h_internal / q_internal) that the __{bam,ham,heap,qam}_db_create calls had already allocated. Since *dbpp is left NULL, the caller can't free it either, so the AM state leaked on any allocation failure during handle creation. Fix: call the (NULL-safe) __{bam,ham,heap,qam}_db_close routines on the err: path before freeing dbp. - __db_join (db_join.c): the err: path freed the join cursor's sub-arrays and the cursor, but not jc->j_key.data (malloc'd) or jc->j_rdata.data (DB_DBT_REALLOC) -- both freed on the normal close path. An allocation failure after j_key.data was set leaked them. Fix: free them on err: too, matching the normal close. Verified: the ASan malloc-injection sweep no longer reports the db_create_internal or db_join leaks; jointest + test001 btree/hash pass. NOTE: the sweep also found a deeper OOM crash (Bug A: __ham_insdel_recover uses a half-rebuilt DB handle with an invalid mpf during txn-undo-at-env-close, crashing in __memp_fget/__db_cursor) -- that is a recovery/dbreg-reopen-under-OOM root cause that needs a proper fix in __dbreg_do_open's error handling, not a per-deref guard (symptom-patching just moves the crash). Documented for a focused follow-up; NOT addressed here.
Coccinelle convention checksNo new violations. ✅ Resolved since baseline (2) -- update dist/cocci/baseline.txt to lock these in. |
ABI diff vs
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two OOM memory leaks found by the extended malloc-fault injection sweep (#90):
__db_create_internal(db_method.c): theerr:path freeddbp+mpfbut not the access-method state (bt_internal/h_internal/q_internal) already allocated by__{bam,ham,heap,qam}_db_create.*dbppis left NULL so the caller can't free it either → leak on any create-time alloc failure. Fix: call the NULL-safe__*_db_closeroutines onerr:.__db_join(db_join.c):err:freed the join cursor's sub-arrays + cursor but notjc->j_key.data(malloc'd) orjc->j_rdata.data(DB_DBT_REALLOC), both freed on normal close. Fix: free them onerr:too.Verified
ASan malloc-injection sweep no longer reports either leak;
jointest+test001 btree/hashpass.Deferred (documented, not in this PR)
The sweep also found Bug A — a deeper OOM crash:
__ham_insdel_recoveruses a half-rebuilt DB handle (invalidmpf) during txn-undo-at-env-close, crashing in__memp_fget. That's a recovery/__dbreg_do_open-under-OOM root cause; symptom-patching the derefs just moves the crash. Documented in.agents/oom-recovery-bug-A.mdfor a focused follow-up (make__dbreg_id_to_dbreturn the OOM error instead of a garbage handle).