Skip to content

fix(ffi): make FFI_ArrowSchema::with_metadata unsafe and guard null private_data - #10764

Open
bit2swaz wants to merge 2 commits into
apache:mainfrom
bit2swaz:fix/ffi-with-metadata-unsafe
Open

fix(ffi): make FFI_ArrowSchema::with_metadata unsafe and guard null private_data#10764
bit2swaz wants to merge 2 commits into
apache:mainfrom
bit2swaz:fix/ffi-with-metadata-unsafe

Conversation

@bit2swaz

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

with_metadata reads self.private_data as a SchemaPrivateData and writes to it, here. but private_data is only a SchemaPrivateData when arrow-rs built the schema. so on any other schema this is undefined behavior, and you can trigger it from safe code two ways:

we cant tell these apart at runtime: the c data interface says private_data is opaque, so theres nothing to check. the fix is to have the caller promise the schema is ours, which is what #10679 landed on

What changes are included in this PR?

Are these changes tested?

added test_with_metadata_on_empty_schema_errors: it calls empty().with_metadata(...) and checks for Err.

ran it under miri with the same -Zmiri-disable-isolation config CI uses. on the old code miri reports UB, after the fix it passes.

the foreign case has no test, you cant call an unsafe fn on a foreign schema in a passing test.

Are there any user-facing changes?

yes, this is breaking:

  • FFI_ArrowSchema::with_metadata is now unsafe, so callers need an unsafe block.
  • on a schema with null private_data it returns an error instead of hitting UB.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-schema labels Aug 19, 2026
Comment thread arrow-schema/src/ffi.rs
S: AsRef<str>,
{
// empty() leaves private_data null; error instead of deref-ing it (#10286).
if self.private_data.is_null() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

im on similar mind to this comment from @alamb

Perhaps it would be more future proof to just set private_data for the result of FFI_ArrowSchema::empty()?

It seems like it is not unreasonable to set metadata on an empty schema, and I worry that other existing (or newly added) code paths will assume private_data is non null

that or set it here if we're adding metadata to an empty schema 🤔

though we can check the other ffi structs to see how they deal with this

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

Labels

arrow Changes to the arrow crate arrow-schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

with_metadata is UB on FFI-imported schemas Null pointer dereference in FFI_ArrowSchema::with_metadata when used with empty schema

2 participants