Skip to content

Commit 0b96dd4

Browse files
committed
tests: lib: mpsc_pbuf: Fix address-of-packed-mem warning
The warning below appears once -Waddress-of-packed-mem is enabled: /__w/zephyr/zephyr/tests/lib/mpsc_pbuf/src/main.c: In function 'item_put_data_overwrite': /__w/zephyr/zephyr/tests/lib/mpsc_pbuf/src/main.c:497:3: error: converting a packed 'struct test_data_ext' pointer (alignment 1) to a 'uint32_t' {aka 'const unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Werror=address-of-packed-member] 497 | mpsc_pbuf_put_data(&buffer, (uint32_t *)&item, len); To avoid the warning, as well as several others related to the same problem, use an intermediate void * variable. More info in #16587. Signed-off-by: Carles Cufi <[email protected]>
1 parent cfbaac6 commit 0b96dd4

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tests/lib/mpsc_pbuf/src/main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,12 @@ void item_put_data_overwrite(bool pow2)
492492
exp_dropped_len[1] = w;
493493

494494
for (uintptr_t i = 0; i < repeat; i++) {
495+
void *vitem;
495496
item.data = (void *)i;
496497
item.hdr.data = i;
497-
mpsc_pbuf_put_data(&buffer, (uint32_t *)&item, len);
498+
vitem = (uint32_t *)&item;
499+
zassert_true(IS_PTR_ALIGNED(vitem, uint32_t), "unaligned ptr");
500+
mpsc_pbuf_put_data(&buffer, (uint32_t *)vitem, len);
498501
}
499502

500503
uint32_t exp_drop_cnt = (sizeof(void *) == sizeof(uint32_t)) ?
@@ -978,6 +981,7 @@ void t_entry(void *p0, void *p1, void *p2)
978981
struct mpsc_pbuf_buffer *buffer = p0;
979982
uintptr_t wait_ms = (uintptr_t)p1;
980983
struct test_data_ext *t;
984+
void *vt;
981985

982986
t = (struct test_data_ext *)mpsc_pbuf_alloc(buffer,
983987
sizeof(*t) / sizeof(uint32_t),
@@ -990,7 +994,9 @@ void t_entry(void *p0, void *p1, void *p2)
990994
t->hdr.len = PUT_EXT_LEN;
991995
t->data = k_current_get();
992996

993-
mpsc_pbuf_commit(buffer, (union mpsc_pbuf_generic *)t);
997+
vt = t;
998+
zassert_true(IS_PTR_ALIGNED(vt, union mpsc_pbuf_generic), "unaligned ptr");
999+
mpsc_pbuf_commit(buffer, (union mpsc_pbuf_generic *)vt);
9941000
while (1) {
9951001
k_sleep(K_MSEC(10));
9961002
}
@@ -1027,6 +1033,7 @@ void test_pending_alloc(void)
10271033
{
10281034
int prio = k_thread_priority_get(k_current_get());
10291035
struct mpsc_pbuf_buffer buffer;
1036+
void *vt;
10301037

10311038
k_thread_priority_set(k_current_get(), 3);
10321039

@@ -1052,7 +1059,9 @@ void test_pending_alloc(void)
10521059

10531060
zassert_true(t, NULL);
10541061
zassert_equal(t->data, tids[ARRAY_SIZE(tids) - 1 - i], NULL);
1055-
mpsc_pbuf_free(&buffer, (union mpsc_pbuf_generic *)t);
1062+
vt = t;
1063+
zassert_true(IS_PTR_ALIGNED(vt, union mpsc_pbuf_generic), "unaligned ptr");
1064+
mpsc_pbuf_free(&buffer, (union mpsc_pbuf_generic *)vt);
10561065
}
10571066

10581067
zassert_equal(mpsc_pbuf_claim(&buffer), NULL, "No more packets.");

0 commit comments

Comments
 (0)