Skip to content

Catch panics and guard null pointers across vortex-ffi - #12331

Open
lwwmanning wants to merge 4 commits into
developfrom
wm/fix-ffi-panic-and-null-guards
Open

lwwmanning wants to merge 4 commits into
developfrom
wm/fix-ffi-panic-and-null-guards

Conversation

@lwwmanning

@lwwmanning lwwmanning commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Several vortex-ffi entrypoints used extern "C-unwind" (allowing Rust panics to unwind across C frames without -fexceptions) or unwrapped extern "C" calls that panicked on null pointers, null slice elements, or invalid decimal parameters. All C FFI entrypoints now use extern "C", catch panics via try_or/try_or_default on error-returning paths, validate pointer arguments before dereferencing, and consume and release passed Arrow C Data Interface structs (vx_array_from_arrow, vx_dtype_from_arrow_schema) on error paths. Value-returning getters without an error channel (vx_dtype_get_variant, vx_dtype_primitive_ptype, vx_scalar_get_*, vx_array_get_bool) keep their documented panic-to-abort behavior on invalid inputs rather than returning misleading fallback values, and the C++ dtype::decimal and Expression::select wrappers throw VortexException on invalid parameters.

Assisted by Antigravity and Devin.

Signed-off-by: Will Manning <will@willmanning.io>
@lwwmanning lwwmanning added the changelog/fix A bug fix label Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T18:54:08.241295Z b7cdabd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vortex-roborev-bot

vortex-roborev-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

roborev: Combined Review (4d68e67)

Verdict: No issues found.


Reviewers: claude-code | Synthesis: claude-code, 0s | Total: 43s, cost partial ~$0.52

@vortex-roborev-bot vortex-roborev-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line comments for the roborev review of b7cdabd. The roborev PR comment has the full review.

Comment thread vortex-ffi/src/dtype.rs Resolved
Comment thread vortex-ffi/src/array.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b7cdabd1af

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vortex-ffi/src/lib.rs Resolved
Comment thread vortex-ffi/src/dtype.rs Resolved
Comment thread vortex-ffi/src/dtype.rs Resolved

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted by Claude Opus 5.5 on Will's behalf.

The switch from C-unwind to C is the right fix. The try_as_ref guards in functions that take error_out are a clear improvement. My concern is the fallback values in functions with no error channel. Before this PR those inputs panicked. Now several return a wrong answer that looks valid.

  • vortex-ffi/src/dtype.rs lines 213 to 220: vx_dtype_get_variant reports DTYPE_NULL for Map, Union, Variant and FixedSizeBinary. Import an Arrow schema with a Map field and walk it. The Map column then reads as a Null column, in the C example and in the C++ DataType::variant(). At line 234, vx_dtype_primitive_ptype returns PTYPE_U8 for any non-primitive dtype. I would let both abort on these inputs, which extern "C" already does for a panic. A new DTYPE_UNKNOWN value also works, but it changes the generated header and the C++ enum.
  • vortex-ffi/src/scalar.rs lines 82 to 337 and vortex-ffi/src/array.rs line 575: the getters now return 0, false or a null view when the input is wrong. For example, vx_scalar_get_decimal_i32 returns 0 for a decimal(12, 0) scalar that holds 100000000000. The doc comments and cinclude/vortex.h still say these functions panic. Please keep the panic here, or name the fallback in each doc comment and regenerate the header.
  • Proportion: many sites wrap bodies that cannot fail on caller input in catch_or. Examples are dtype.rs lines 104 and 110, scalar.rs line 72, expression.rs line 62 and lib.rs line 178. Under extern "C", a panic there already aborts. Dropping catch_or from these sites removes a large share of the added lines.
  • vortex-ffi/src/dtype.rs line 198: vx_dtype_new_decimal now returns NULL with no error for invalid precision or scale. The C++ wrapper at lang/cpp/src/dtype.cpp line 177 adopts that NULL, so the C++ DataType fails later, far from the cause. Please throw in the C++ wrapper when the call returns NULL.

…guard C++ decimal

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed all four points in d8899d293:

  • vx_dtype_get_variant and vx_dtype_primitive_ptype: restored panic-to-abort behavior on null/unsupported/non-primitive dtypes instead of returning DTYPE_NULL or PTYPE_U8.
  • vx_scalar_get_* and vx_array_get_bool: restored the documented panic-to-abort behavior on null/mismatched/overflowing/out-of-bounds inputs so vortex.h docs and behavior stay aligned.
  • Removed catch_or completely across vortex-ffi (dropping it from infallible bodies as well; panics under extern "C" abort).
  • lang/cpp/src/dtype.cpp: dtype::decimal now checks for nullptr from vx_dtype_new_decimal and throws VortexException immediately, with unit test coverage in lang/cpp/tests/dtype.cpp.

@vortex-roborev-bot vortex-roborev-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line comments for the roborev review of d8899d2. The roborev PR comment has the full review.

Comment thread vortex-ffi/src/dtype.rs Resolved

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted by Claude Opus 5.5 on Will's behalf.

Commit d8899d2 resolves my earlier review. The getters with no error channel panic again, so they abort instead of returning a value that looks valid. That covers vx_dtype_get_variant, vx_dtype_primitive_ptype, the scalar getters and vx_array_get_bool. The C++ decimal wrapper now throws when the C call returns a null pointer, and with catch_or gone the diff is in proportion. One optional nit: the doc comment at vortex-ffi/src/dtype.rs line 180 could say that vx_dtype_new_decimal returns a null pointer for an invalid precision or scale. That change also needs a header regeneration.

…pe_new_decimal

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Updated the doc comment on vx_dtype_new_decimal and vortex-ffi/cinclude/vortex.h to document the NULL return on invalid precision or scale.

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Gemini 4 Carbon on behalf of @lwwmanning.

Switching all vortex-ffi entrypoints to extern "C" and keeping panic-to-abort on the infallible getters looks good. Two concrete contract/wrapper bugs need fixes before merge:

  1. Arrow C Data Interface leak on null/error paths (vx_array_from_arrow and vx_dtype_from_arrow_schema):
    • Both functions document in vortex.h that array and schema are unconditionally consumed (their release callbacks are invoked by the callee and the caller must not release them afterwards). Unlike vx_partition_scan_arrow and vx_struct_fields_builder_add_field, vx_array_from_arrow and vx_dtype_from_arrow_schema currently check vx_session::try_as_ref(session)? (and the sibling pointer) before ptr::replace(..., FFI_Arrow*::empty()). Now that a null session (or mixed-null (array, NULL) / (NULL, schema) call) returns NULL + error_out instead of aborting, any valid FFI_ArrowArray or FFI_ArrowSchema passed to the call is left unreleased and leaked.
  2. C++ Expression::select adopts nullptr on invalid UTF-8 (lang/cpp/src/expression.cpp):
    • vx_expression_select now returns NULL instead of panicking when names contains invalid UTF-8, but select_impl in lang/cpp/src/expression.cpp:54 passes the result straight to Access::adopt<Expression>(...) without checking out == nullptr (unlike Expression::operator[] right above it and dtype::decimal), constructing a C++ Expression wrapping a null handle. vx_expression_select's doc comment and vortex.h also omit its NULL return contract.

Remediation Plan

  1. vortex-ffi/src/array.rs (vx_array_from_arrow): Move out whichever of array and schema is non-null via ptr::replace(..., FFI_ArrowArray::empty()) / ptr::replace(..., FFI_ArrowSchema::empty()) before checking for null array, null schema, or vx_session::try_as_ref(session)? (matching vortex-jni-native/src/writer.rs's writeBatch), so Drop invokes the exporter's release callback on every error path. Add an assertion in vortex-ffi tests verifying release is cleared when vx_array_from_arrow fails on a null session.
  2. vortex-ffi/src/dtype.rs (vx_dtype_from_arrow_schema): After vortex_ensure!(!schema.is_null(), "null arrow schema"), immediately take ownership via let ffi_schema = unsafe { ptr::replace(schema, FFI_ArrowSchema::empty()) }; before vx_session::try_as_ref(session)?, and add a test assertion that schema.release is cleared when session is null.
  3. lang/cpp/src/expression.cpp (select_impl) & vortex-ffi/src/expression.rs (vx_expression_select):
    • In select_impl (lang/cpp/src/expression.cpp:54), check if (out == nullptr) { throw VortexException("select: field name is not valid UTF-8"); } before Access::adopt<Expression>(out), and add a REQUIRE_THROWS_AS case in lang/cpp/tests/ (mirroring dtype.cpp's invalid UTF-8 test).
    • Document on vx_expression_select in vortex-ffi/src/expression.rs that it returns NULL if child is null, names is null with len > 0, or any field name is not valid UTF-8, and regenerate vortex-ffi/cinclude/vortex.h.

…ion::select

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed both findings:

  1. Arrow C Data Interface release on error paths: vx_array_from_arrow and vx_dtype_from_arrow_schema now move out any non-null FFI_ArrowArray / FFI_ArrowSchema via ptr::replace(..., empty()) before null/session checks so Drop always invokes release on error paths, with unit tests verifying is_released() / release().is_none() on null session and mixed-null calls.
  2. C++ Expression::select & vx_expression_select docs: select_impl in lang/cpp/src/expression.cpp now checks for nullptr and throws VortexException (tested in lang/cpp/tests/scan.cpp), and vx_expression_select in vortex-ffi/src/expression.rs and vortex-ffi/cinclude/vortex.h documents the NULL return contract.

This branch has not been deployed

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

Labels

changelog/fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant