feat: restore ldb_query_raw shared-library query entry point - #60
Merged
Merged
Conversation
The new_hash refactor dropped src/ldb_wrapper.c, which provided ldb_query_raw — the single-key shared-library entry point third-party consumers link against (e.g. the Go cgo binding in scanoss.api). The CLI (shell/command) is the only query surface left on this branch, so programmatic consumers of libldb.so have no entry point. Restore the wrapper, adapted to the new_hash / crc64 architecture: - ldb_dump_row matches the current ldb_record_handler_t signature (takes struct ldb_table *, no int subkey_ln) and frames each record as [uint32 size][payload] into a growable T_RawRes. - Key length is validated against the table's .cfg key_ln (or the 32-bit main key), so both MD5 (16-byte) and CRC64 (8-byte) tables work unchanged. Hardening over the historical version: - Do not free the caller-owned dbtable argument (ldb_read_cfg copies it; it was never ours to free). - Replace exit(EXIT_FAILURE) on a failed realloc with a graceful stop that returns the partial result — a library must not abort the host process. - Fix the growth check to account for the 4-byte size prefix (the old check compared payload size only but wrote size+4, a potential 4-byte overrun). - Route diagnostics through logger (log_info) instead of printf, and drop an unused buffer allocation. Picked up automatically by the Makefile's src/*.c wildcard; libldb.so exports ldb_query_raw and ldb_dump_row. Verified end-to-end against a real CRC64 KB (ossv2 pivot/file/path chain) via the downstream scanoss.api LDB read path.
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.
Why
The
new_hashrefactor removedsrc/ldb_wrapper.c, which definedldb_query_raw— the single-key shared-library entry point that third-party consumers linklibldb.soagainst (e.g. the Go cgo binding used byscanoss.api). On this branch the only remaining query surface is the interactive CLI (shell/command), so a process embeddinglibldb.sohas no programmatic way to read a table. This restores that entry point, adapted to the current architecture.What
Two additive files (
src/ldb_wrapper.{c,h}), picked up automatically by the Makefile'ssrc/*.cwildcard;libldb.sothen exportsldb_query_rawandldb_dump_row.ldb_query_raw(dbtable, key)— validates the table, reads its.cfg, checks the hex key against the table'skey_ln(or the 32-bit main key), and collects every matching record vialdb_fetch_recordset.ldb_dump_row(...)— record handler matching the currentldb_record_handler_tsignature (struct ldb_table *first arg, noint subkey_ln). Frames each record as[uint32 size][payload]into a growableT_RawRes.Because key length is
.cfg-driven, both MD5 (16-byte) and CRC64 (8-byte) tables work with no code change.Hardening over the historical wrapper
The version that existed pre-
new_hashhad a few rough edges; this restores it fixed:free()s the caller-owneddbtableargument (ldb_read_cfgcopies it internally — it was never the callee's to free).exit()in a library: a failedreallocnow stops iteration and returns the partial result instead ofexit(EXIT_FAILURE)killing the host process.size + 4— a potential 4-byte overrun at the boundary).logger(log_info) instead ofprintf; dropped an unused allocation.Testing
make/make lib(-Wall, no new warnings);objdump -T libldb.soshowsldb_query_raw/ldb_dump_rowexported, and theldbCLI links unchanged.ossv2pivot → file → pathchain) through the downstreamscanoss.apiLDB read path.Follow-ups (out of scope here)
ldb_query_raw(the currenttest/suite exercises the CLI viabash_unit; there's precedent intest/null_sector_test.cfor a C test).dbtablemust free their own argument after re-vendoring (the correct ownership).libldb.soshould target the oldest supported glibc for portability across consumer hosts.