Skip to content

Rust Push::alignment Incorrect for Structs #8150

Closed
@tustvold

Description

@tustvold

Consider a struct of the form

struct FieldNode {
  length: long;
  null_count: long;
}

The following code is generated for Push

impl<'b> flatbuffers::Push for FieldNode {
    type Output = FieldNode;
    #[inline]
    unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
        let src =
            ::core::slice::from_raw_parts(self as *const FieldNode as *const u8, Self::size());
        dst.copy_from_slice(src);
    }
}

This therefore uses the default impl of Push::alignment which is

fn alignment() -> PushAlignment {
    PushAlignment::new(align_of::<Self::Output>())
}

Unfortunately the definition of FieldNode is

pub struct FieldNode(pub [u8; 16]);

Which has an alignment of 1.

The net result is that the writer does not provide the correct alignment guarantees for structs, which causes the verifiers of some implementations to fail - apache/arrow-rs#5052.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions