Check malloc return values in C API (#13099)#14879
Conversation
Several C API functions allocate buffers with malloc() and immediately dereference the result without checking for failure. On a low-memory system malloc() can return NULL, causing a null-pointer dereference and crash in the following loop. Add NULL checks to the three call sites that have an errptr to propagate the failure, mirroring the existing pattern used by rocksdb_open_and_compact: - rocksdb_list_column_families - rocksdb_create_column_families (also frees the already-created column family handles to avoid leaking them on allocation failure) - rocksdb_load_latest_options (frees the first buffer if the second allocation fails) On failure these now report Status::MemoryLimit through errptr and return nullptr (with the out-params reset) instead of crashing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
✅ clang-tidy: No findings on changed linesCompleted in 52.0s. |
🟡 Codex Code ReviewAuto-triggered after CI passed — reviewing commit f377050 ❌ Codex review failed before producing findings. ℹ️ About this responseGenerated by Codex CLI. Limitations:
Commands:
|
✅ Claude Code ReviewAuto-triggered after CI passed — reviewing commit f377050 SummaryClean, well-scoped defensive fix that adds NULL checks after three High-severity findings (0): Full review (click to expand)Findings🔴 HIGHNone. 🟡 MEDIUMM1. Orphaned column families in
|
Summary
Fixes part of #13099.
Several C API functions in
db/c.ccallocate buffers withmalloc()and immediately dereference the result in the following loop without checking for failure. On a low-memory systemmalloc()can returnNULL, which leads to a null-pointer dereference and a crash.This PR adds
NULLchecks to the three call sites that already take anerrptrparameter, so the allocation failure can be propagated cleanly. The handling mirrors the existing pattern already used byrocksdb_open_and_compact(reportStatus::MemoryLimitthrougherrptrand returnnullptr).Call sites fixed
rocksdb_list_column_families— sets*lencfs = 0, reports the error and returnsnullptr.rocksdb_create_column_families— the underlying column families have already been created at this point, so on allocation failure it deletes the created handles to avoid leaking them before returning.rocksdb_load_latest_options— has two allocations; if either fails it frees the other buffer (free(nullptr)is a safe no-op), resets all out-params, reports the error and returns.Scope
This intentionally covers only the call sites that have an
errptrto propagate the failure, keeping the change small and consistent with the existing error-handling pattern. Other uncheckedmallocsites mentioned in the issue (e.g. functions without anerrptr, theCopyStringhelper, andcache/lru_cache.cc) can be addressed in follow-ups since they need different handling.Test plan
make format-auto/build_tools/format-diff.sh -c— clean.make check-sources— passes.db/c.ocompiles.The new branches are only taken under allocation failure, which is not exercised by the existing functional tests; the change preserves existing behavior on the success path.