Re: [PATCH] selftests/user_events: Avoid taking address of packed member in perf_test

Andrew Morton
2025-10-28T20:26:10Z

On Tue, 28 Oct 2025 22:28:10 +0530 Ankit Khushwaha <ankitkhushwaha.linux@gmail.com> wrote:

> > > @@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
> > > ASSERT_EQ(1 << reg.enable_bit, self->check);
> > >
> > > /* Ensure write shows up at correct offset */
> > > - ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
> > > + memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
> > > + ASSERT_NE(-1, write(self->data_fd, &write_index,
> > > sizeof(reg.write_index)));
> >
> > Simply casting &write_index to void* would fix this?
>
> yes, this hides the type mismatch from the compiler. But i think
> casting to void * will not fix the alignment mismatch for packed struct.
> It works on x86, but might break on other platform.

It's the second argument to write(2)! write(2) expects a const char *,
but void* will work.