Skip to content

fix: validate C enum inputs at the FFI boundary - #86

Merged
Xuanwo merged 7 commits into
mainfrom
xuanwo/fix-c-enum-validation
Nov 21, 2026
Merged

Xuanwo merged 7 commits into
mainfrom
xuanwo/fix-c-enum-validation

Conversation

@Xuanwo

@Xuanwo Xuanwo commented Nov 21, 2026

Copy link
Copy Markdown
Member

Summary

Take the existing C enum inputs as c_int at the Rust FFI boundary and validate them with from_c_int before constructing Rust enums. This removes undefined behavior when a C caller passes a value outside the public header's enumerators.

Covered inputs:

  • lancedb_table_create_vector_index and lancedb_table_create_scalar_index index_type
  • LanceDBVectorIndexConfig.distance_type
  • lancedb_table_optimize optimize_type
  • lancedb_vector_query_distance_type distance_type
  • lancedb_expr_binary op

The public C header layout and signatures are unchanged. Invalid values keep the existing error conventions: LANCEDB_INVALID_ARGUMENT where an error code is returned, and NULL for lancedb_expr_binary.

The change also adds C consumer regression tests that pass out-of-range values through the public C ABI, plus compile-time checks that the C enums and LanceDBVectorIndexConfig keep the ABI the Rust layer expects. The helper is compiled as C17.

Addresses the existing-main FFI inputs described in #75. No changes to #74.

Validation

  • cargo check --lib
  • cargo clippy --lib
  • CMake configure with BUILD_EXAMPLES=ON, BUILD_TESTS=ON, CMAKE_BUILD_TYPE=Release
  • ninja -C build -j 12
  • ctest --test-dir build -j 4: all 9 test suites passed, including the valgrind-backed index and DataFusion expression suites
  • Focused invalid-input test cases in lancedb_index_tests, lancedb_vector_query_tests, and lancedb_df_query_tests
  • GCC and Clang C17 -Werror compile of the C consumer helper

Take the existing C enum parameters and the vector index config field as
c_int in Rust, validate them before constructing Rust enums, and keep the
public header and established error conventions unchanged.

Add C consumer regression tests for out-of-range index, distance,
optimize and binary-op values. The helper is compiled as C17 against the
public header.
C_STANDARD on a source file is ignored by CMake. Set C_STANDARD,
C_STANDARD_REQUIRED and C_EXTENSIONS on the three targets that compile
the C consumer helper, leaving their C++ sources and all other targets
unchanged.
Stop scheduling the removed macos-15-intel packaging job. The
standalone C packaging change will remove the remaining script and
documentation references.
@Xuanwo
Xuanwo merged commit 809e2fb into main Nov 21, 2026
12 checks passed
Xuanwo added a commit that referenced this pull request Nov 21, 2026
Reuse the checked enum conversions added by #86:
LanceDBIndexType::from_c_int for the create index type and
LanceDBDistanceType::from_c_int behind the optional -1 distance marker.

Add a C17 helper for lancedb_table_create_index so the out-of-range index
value stays an ordinary C integer, then assert the call is rejected and
leaves the index list empty. The other generic-create and index-list cases
are unchanged.
Xuanwo added a commit that referenced this pull request Nov 21, 2026
* index: create indexes with names, waits and tuning options

Add lancedb_table_create_index, a single additive entry point that covers
the SDK's common index creation options and the per-type tuning required by
the published and current Go index APIs.

The new LanceDBIndexOptions keeps omission distinct from explicit values:
numeric fields use -1 for unset, boolean fields use -1 for unset and 0/1
for false/true, and strings use NULL for unset. This lets callers leave one
FTS flag at the SDK default while explicitly setting another to false, which
the existing per-type config structs cannot express. Numeric parameters are
carried as int64 so every uint32 value and the -1 sentinel fit without
truncation.

The entry point also exposes the SDK index name, replace choice and
builder-level wait_timeout. The wait is a polling budget, not a wall-clock
bound, and native tables ignore it. LANCEDB_INDEX_AUTO remains the SDK Auto
index rather than the Go Auto-to-IVF-PQ mapping.

Raw index and distance enums are validated before conversion. Invalid
ranges, invalid UTF-8, unsupported index types and SDK errors are returned
with their messages. Existing create signatures, defaults and Go code are
unchanged.

Consumer tests cover a name distinct from the column, explicit replace false
versus true, FTS and ngram options, vector IVF/PQ/HNSW tuning, cosine versus
L2 changing a real query result, and invalid input ranges.

* test: make index metric regression deterministic

Replace the nearly collinear 256-row fixture with four controlled vectors
whose L2 and cosine winners are far apart. Query without setting a metric,
so the SDK uses the metric of the built index. The cosine case fails and
returns row_0 if the creation option is dropped.

Also document the actual remote create wait: servers that return a job ID use
the SDK timeout on that job, legacy servers fall back to polling the named
index, and reaching the budget does not cancel the submitted build. Native
tables remain synchronous and ignore the option.

* ci: drop retired Intel macOS package job

* test: drop out-of-range C++ index enum cast

* docs: describe actual SDK create wait behavior

* docs: remove redundant create wait sentence

* test: cover generic index creation options

* test: cover generic create invalid index enum

Reuse the checked enum conversions added by #86:
LanceDBIndexType::from_c_int for the create index type and
LanceDBDistanceType::from_c_int behind the optional -1 distance marker.

Add a C17 helper for lancedb_table_create_index so the out-of-range index
value stays an ordinary C integer, then assert the call is rejected and
leaves the index list empty. The other generic-create and index-list cases
are unchanged.
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