public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
* [PATCH] selftests/user_events: Avoid taking address of packed member in perf_test
@ 2025-10-27 11:34 Ankit Khushwaha
  2025-10-27 23:25 ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Ankit Khushwaha @ 2025-10-27 11:34 UTC (permalink / raw)
  To: linux-kselftest, Shuah Khan
  Cc: Bala-Vignesh-Reddy, Andrew Morton, Wei Yang, linux-kernel,
	linux-kernel-mentees, Ankit Khushwaha

Accessing 'reg.write_index' directly triggers a -Waddress-of-packed-member
warning due to potential unaligned pointer access:

perf_test.c:239:38: warning: taking address of packed member 'write_index'
of class or structure 'user_reg' may result in an unaligned pointer value 
[-Waddress-of-packed-member]
  239 |         ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
      |                                             ^~~~~~~~~~~~~~~ 

Use memcpy() instead to safely copy the value and avoid unaligned pointer
access across architectures.

Signed-off-by: Ankit Khushwaha <ankitkhushwaha.linux@gmail.com>
---
 tools/testing/selftests/user_events/perf_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/user_events/perf_test.c b/tools/testing/selftests/user_events/perf_test.c
index 201459d8094d..e4385f4aa231 100644
--- a/tools/testing/selftests/user_events/perf_test.c
+++ b/tools/testing/selftests/user_events/perf_test.c
@@ -201,6 +201,7 @@ TEST_F(user, perf_empty_events) {
 	struct perf_event_mmap_page *perf_page;
 	int page_size = sysconf(_SC_PAGESIZE);
 	int id, fd;
+	__u32 write_index;
 	__u32 *val;
 
 	reg.size = sizeof(reg);
@@ -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)));
 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-11-01  3:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-27 11:34 [PATCH] selftests/user_events: Avoid taking address of packed member in perf_test Ankit Khushwaha
2025-10-27 23:25 ` Andrew Morton
2025-10-28 16:58   ` Ankit Khushwaha
2025-10-28 20:26     ` Andrew Morton
2025-10-29 15:20       ` Ankit Khushwaha
2025-11-01  3:19         ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox