-
Notifications
You must be signed in to change notification settings - Fork 796
Generated tests use UB in their calculations #1651
Copy link
Copy link
Closed
Labels
Description
Test code for struct offsets run checks like this:
assert_eq!(
unsafe { &(*(::core::ptr::null::<timeval>())).tv_sec as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(timeval),
"::",
stringify!(tv_sec)
)
);But it's UB to dereference a null pointer, and it's also UB to create a &T with numeric value 0.
There is actually trivial to fix. Instead of using a null pointer, just create a value on the stack (using core::mem::zeroed(), which is safe since all C structs can be created as zeroed-memory), and then use a reference to that stack value, the fields of that stack value, and so on.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Type
Fields
Give feedbackNo fields configured for issues without a type.