Skip to content

Commit

Permalink
Update the fix for #99 and some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson committed Jun 13, 2022
1 parent b0bce70 commit 2553355
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 14 deletions.
30 changes: 16 additions & 14 deletions planus/src/table_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,23 @@ impl<'buf> Table<'buf> {
error_kind,
};

let offset = self
.vtable
.get(2 * vtable_offset..2 * (vtable_offset + 2))
.ok_or_else(|| make_error(ErrorKind::InvalidOffset))?;

let tag_offset = u16::from_le_bytes(offset[..2].try_into().unwrap()) as usize;
let value_offset = u16::from_le_bytes(offset[2..].try_into().unwrap()) as usize;

let tag = u8::from_buffer(self.object, tag_offset).map_err(make_error)?;
if tag_offset != 0 && value_offset != 0 && tag != 0 {
T::from_buffer(self.object, value_offset, tag)
.map(Some)
.map_err(make_error)
} else {
if let Some(offset) = self.vtable.get(2 * vtable_offset..2 * (vtable_offset + 2)) {
let tag_offset = u16::from_le_bytes(offset[..2].try_into().unwrap()) as usize;
let value_offset = u16::from_le_bytes(offset[2..].try_into().unwrap()) as usize;
let tag = u8::from_buffer(self.object, tag_offset).map_err(make_error)?;
if tag_offset != 0 && value_offset != 0 && tag != 0 {
T::from_buffer(self.object, value_offset, tag)
.map(Some)
.map_err(make_error)
} else {
Ok(None)
}
} else if self.vtable.len() <= 2 * vtable_offset {
Ok(None)
} else {
Err(make_error(ErrorKind::InvalidVtableLength {
length: self.vtable.len() as u16 + 4,
}))
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/rust/test_files/issue_99.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
table Table {}

union Union {
t: Table
}

table Root {
f: Union;
}
Binary file not shown.
14 changes: 14 additions & 0 deletions test/rust/test_files/issue_99/deserialize/bad-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
RootRef {
f: Err(
Error {
source_location: ErrorLocation {
type_: "Root",
method: "f",
byte_offset: 10,
},
error_kind: InvalidVtableLength {
length: 6,
},
},
),
}
Binary file not shown.
1 change: 1 addition & 0 deletions test/rust/test_files/issue_99/deserialize/issue-99.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RootRef
Binary file not shown.
4 changes: 4 additions & 0 deletions test/rust/test_files/issue_99/serialize/non_null.dbg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Root {
f_type: t,
f: Table,
}
6 changes: 6 additions & 0 deletions test/rust/test_files/issue_99/serialize/non_null.dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
obj @ 0x04..0x0d
vtable @ 0x10..0x18
field[0] @ 0x0c..0x0d:
01
field[1] @ 0x08..0x0c:
10 00 00 00
1 change: 1 addition & 0 deletions test/rust/test_files/issue_99/serialize/non_null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"f": {"T": {}}}
Binary file added test/rust/test_files/issue_99/serialize/null.bin
Binary file not shown.
4 changes: 4 additions & 0 deletions test/rust/test_files/issue_99/serialize/null.dbg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Root {
f_type: NONE,
f: None,
}
2 changes: 2 additions & 0 deletions test/rust/test_files/issue_99/serialize/null.dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
obj @ 0x04..0x08
vtable @ 0x08..0x0c
1 change: 1 addition & 0 deletions test/rust/test_files/issue_99/serialize/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"f": null}

0 comments on commit 2553355

Please sign in to comment.