Skip to content

Commit cf34afd

Browse files
nmeumnashif
authored andcommitted
testsuite: coverage: fix -Wcast-align warning
While porting the coverage.c file from RIOT to Zephyr, which employs different compiler flags, I noticed several -Wcast-align GCC warnings on arm. I think, as is, the current implementation may perform unaligned memory accesses which may not be supported on certain platforms. To workaround that, I have rewritten the code for RIOT using bytewise-writes with `memcpy`. Signed-off-by: Sören Tempel <[email protected]>
1 parent 0a7a61f commit cf34afd

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

subsys/testsuite/coverage/coverage.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdio.h>
99
#include <stdint.h>
1010
#include <errno.h>
11+
#include <string.h>
1112
#include "coverage.h"
1213

1314
K_HEAP_DEFINE(gcov_heap, CONFIG_COVERAGE_GCOV_HEAP_SIZE);
@@ -38,12 +39,9 @@ void __gcov_exit(void)
3839
* buff_write_u64 - Store 64 bit data on a buffer and return the size
3940
*/
4041

41-
#define MASK_32BIT (0xffffffffUL)
4242
static inline void buff_write_u64(void *buffer, size_t *off, uint64_t v)
4343
{
44-
*((uint32_t *)((uint8_t *)buffer + *off) + 0) = (uint32_t)(v & MASK_32BIT);
45-
*((uint32_t *)((uint8_t *)buffer + *off) + 1) = (uint32_t)((v >> 32) &
46-
MASK_32BIT);
44+
memcpy((uint8_t *)buffer + *off, (uint8_t *)&v, sizeof(v));
4745
*off = *off + sizeof(uint64_t);
4846
}
4947

@@ -52,7 +50,7 @@ static inline void buff_write_u64(void *buffer, size_t *off, uint64_t v)
5250
*/
5351
static inline void buff_write_u32(void *buffer, size_t *off, uint32_t v)
5452
{
55-
*((uint32_t *)((uint8_t *)buffer + *off)) = v;
53+
memcpy((uint8_t *)buffer + *off, (uint8_t *)&v, sizeof(v));
5654
*off = *off + sizeof(uint32_t);
5755
}
5856

0 commit comments

Comments
 (0)