Skip to content

Commit e99f502

Browse files
committed
fix: set struct align as 8
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
1 parent c7f3c88 commit e99f502

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

examples/hifive-unmatched-a00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct Cpu<'a> {
4949
const RAW_DEVICE_TREE: &[u8] = include_bytes!("hifive-unmatched-a00.dtb");
5050
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
5151

52-
#[repr(align(4))]
52+
#[repr(align(8))]
5353
struct AlignedBuffer {
5454
pub data: [u8; RAW_DEVICE_TREE.len()],
5555
}

examples/qemu-virt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde_device_tree::{
1919
const RAW_DEVICE_TREE: &[u8] = include_bytes!("qemu-virt.dtb");
2020
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
2121

22-
#[repr(align(4))]
22+
#[repr(align(8))]
2323
struct AlignedBuffer {
2424
pub data: [u8; RAW_DEVICE_TREE.len()],
2525
}

src/de.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777

7878
let total_size = u32::from_be(header.total_size);
7979
let raw_data_len = (total_size - HEADER_LEN) as usize;
80-
let ans_ptr = core::ptr::from_raw_parts(ptr as *const (), raw_data_len);
80+
let ans_ptr = core::ptr::from_raw_parts(ptr as *const u8, raw_data_len);
8181
let device_tree: &DeviceTree = &*ans_ptr;
8282
let tags = device_tree.tags();
8383
let mut d = Deserializer {
@@ -522,7 +522,16 @@ mod tests {
522522
#[test]
523523
fn error_invalid_magic() {
524524
static DEVICE_TREE: &[u8] = &[0x11, 0x22, 0x33, 0x44]; // not device tree blob format
525-
let ptr = DEVICE_TREE.as_ptr();
525+
const DEVICE_TREE_LEN: usize = DEVICE_TREE.len();
526+
#[repr(align(8))]
527+
struct AlignedBuffer {
528+
pub data: [u8; DEVICE_TREE_LEN],
529+
}
530+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
531+
data: [0; DEVICE_TREE_LEN],
532+
});
533+
aligned_data.data[..DEVICE_TREE_LEN].clone_from_slice(DEVICE_TREE);
534+
let ptr = aligned_data.data.as_ptr();
526535

527536
#[derive(Debug, Deserialize)]
528537
struct Tree {}

0 commit comments

Comments
 (0)