You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
before cloning a rust struct holding an enum coming from C, we must verify that the enum has a valid value.
C enums are just integers, and code would compile if an invalid value is passed
Checked main at 6071a08. The same unsoundness applies to every C enum passed by value, not only to cloned structs: Rust assumes the discriminant is valid before the function body runs, so a match fallback cannot catch it.
Main input positions:
LanceDBVectorIndexConfig.distance_type (*config is cloned in lancedb_table_create_vector_index)
lancedb_expr_binary(..., LanceDBBinaryOp, ...) (returns a pointer, so the existing failure form is NULL)
Realistic callers hit this with an uninitialized or partially initialized config, an arbitrary -1/out-of-range int, or a caller built against a newer header. The result is undefined behavior, not a reliable LANCEDB_INVALID_ARGUMENT: an out-of-range optimize type can dispatch to a different action, the query path can store a bogus distance, and either can crash.
#74 does not fix these: its _ arms only reject valid-but-wrong variants, and it adds LanceDBOptimizeIndexOptions.mode as another enum input cloned before validation (the review thread deferred that case here). Its new index values 9-12 also add a header/library skew case.
Next step: after #74, a separate bounded change should accept c_int at the Rust FFI boundary, validate before converting or cloning, and return the existing error code (NULL for lancedb_expr_binary). The C header and ABI can stay unchanged; regression tests should pass out-of-range ints to each entry point.
Activity
Xuanwo commentedon Nov 21, 2026
Checked
mainat 6071a08. The same unsoundness applies to every C enum passed by value, not only to cloned structs: Rust assumes the discriminant is valid before the function body runs, so amatchfallback cannot catch it.Main input positions:
LanceDBVectorIndexConfig.distance_type(*configis cloned inlancedb_table_create_vector_index)lancedb_table_create_vector_index(..., LanceDBIndexType, ...)lancedb_table_create_scalar_index(..., LanceDBIndexType, ...)lancedb_table_optimize(..., LanceDBOptimizeType, ...)lancedb_vector_query_distance_type(..., LanceDBDistanceType, ...)lancedb_expr_binary(..., LanceDBBinaryOp, ...)(returns a pointer, so the existing failure form is NULL)Realistic callers hit this with an uninitialized or partially initialized config, an arbitrary
-1/out-of-range int, or a caller built against a newer header. The result is undefined behavior, not a reliableLANCEDB_INVALID_ARGUMENT: an out-of-range optimize type can dispatch to a different action, the query path can store a bogus distance, and either can crash.#74 does not fix these: its
_arms only reject valid-but-wrong variants, and it addsLanceDBOptimizeIndexOptions.modeas another enum input cloned before validation (the review thread deferred that case here). Its new index values 9-12 also add a header/library skew case.Next step: after #74, a separate bounded change should accept
c_intat the Rust FFI boundary, validate before converting or cloning, and return the existing error code (NULL forlancedb_expr_binary). The C header and ABI can stay unchanged; regression tests should pass out-of-range ints to each entry point.