Skip to content

[Rust] 'Unaligned' error when reading a newly created buffer #6359

@sergiimk

Description

@sergiimk

After upgrading to flatbuffers = "0.8.0" and flatc 1.12.0 (master) I started getting occasional "Unaligned" errors when trying to read back a flatbuffer that was just written.

Example schema:

table MetadataBlock {
  block_hash: [ubyte];
}

Example code:

mod fb_generated;
use fb_generated::*;

fn main() {
    let mut fb = flatbuffers::FlatBufferBuilder::new_with_capacity(33);  // Happens without odd capacity on some of my objects too
    let block_hash: Vec<u8> = vec![0, 10];
    let block_hash_offset = fb.create_vector(&block_hash);
    let mut builder = MetadataBlockBuilder::new(&mut fb);
    builder.add_block_hash(block_hash_offset);
    let offset = builder.finish();
    fb.finish(offset, None);
    let (buf, head) = fb.collapse();
    println!("{} {:?}", head, buf);

    flatbuffers::root::<MetadataBlock>(&buf[head..]).unwrap();
}

Produces:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Unaligned { position: 0, unaligned_type: "u32", error_trace: ErrorTrace([]) }', src/main.rs:15:54

The previous version flatbuffers = "0.7.0" had no such issue. Albeit flatbuffers::get_root was not returning a Result before all data was read correctly.

Is it reasonable to expect fb itself to produce a properly aligned data?

Metadata

Metadata

Assignees

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