Skip to content

fix(db): free handle/join-cursor state on create-time OOM error paths - #92

Merged
gburd merged 1 commit into
masterfrom
fix/oom-leaks-create-join
Jul 30, 2026
Merged

fix(db): free handle/join-cursor state on create-time OOM error paths#92
gburd merged 1 commit into
masterfrom
fix/oom-leaks-create-join

Conversation

@gburd

@gburd gburd commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Two OOM memory leaks found by the extended malloc-fault injection sweep (#90):

  • __db_create_internal (db_method.c): the err: path freed dbp+mpf but not the access-method state (bt_internal/h_internal/q_internal) already allocated by __{bam,ham,heap,qam}_db_create. *dbpp is left NULL so the caller can't free it either → leak on any create-time alloc failure. Fix: call the NULL-safe __*_db_close routines on err:.
  • __db_join (db_join.c): err: freed the join cursor's sub-arrays + cursor but not jc->j_key.data (malloc'd) or jc->j_rdata.data (DB_DBT_REALLOC), both freed on normal close. Fix: free them on err: too.

Verified

ASan malloc-injection sweep no longer reports either leak; jointest + test001 btree/hash pass.

Deferred (documented, not in this PR)

The sweep also found Bug A — a deeper OOM crash: __ham_insdel_recover uses a half-rebuilt DB handle (invalid mpf) 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.md for a focused follow-up (make __dbreg_id_to_db return the OOM error instead of a garbage handle).

…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.
@gburd
gburd merged commit b315b7a into master Jul 30, 2026
47 of 49 checks passed
@gburd
gburd deleted the fix/oom-leaks-create-join branch July 30, 2026 11:58
@github-actions

Copy link
Copy Markdown

Coccinelle convention checks

No new violations. ✅

Resolved since baseline (2) -- update dist/cocci/baseline.txt to lock these in.
rule_mutex_unbalanced|MUTEX_UNBALANCED|src/crypto/mersenne/mt19937db.c|return (ret);
rule_mutex_unbalanced|MUTEX_UNBALANCED|src/mp/mp_register.c|return (ret);

@github-actions

Copy link
Copy Markdown

ABI diff vs v5.3.32 (libabigail — authoritative)

Removed exported symbols (nm -D, _NNNN version suffix normalized)

None.


Advisory: libabigail/nm is the authoritative binary-ABI check; Coccinelle is complementary source-level early warning. See dist/cocci/README.md.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant