Skip to content

verify C enums before cloning #75

Open

Description

@yuvalif

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

Activity

  1. added
    bug
    on Nov 15, 2026
  2. Xuanwo commented on Nov 21, 2026

    @Xuanwo
    Member

    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_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 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions