[riot-notifications] [RIOT-OS/RIOT] sam0_common/periph/flashpage: fix unaligned write (#15707)

Marian Buschsieweke notifications at github.com
Tue Jan 19 17:04:56 CET 2021

@maribu commented on this pull request.

> @@ -223,21 +230,25 @@ static void _write_page(void* dst, const void *data, size_t len, void (*cmd_writ
     /* write the first, unaligned bytes */
-    if (unaligned_start) {
-        *dst32++ = unaligned_pad_start(data, unaligned_start);
-        data = (uint8_t*)data + unaligned_start;
+    if (len_unaligned_start) {
+        *dst32++ = unaligned_pad_start(data, len_unaligned_start,
+                offset_unaligned_start);
+        data = (uint8_t*)data + len_unaligned_start;
     /* copy whole words */
     const uint32_t *data32 = data;

I think you misunderstood what I meant. Please continue to use `unaligned_get_u32()`, but do not use `uint32_t *` as type. An `uint32_t *` must point to a RAM address that fulfills the alignment requirement of `uint32_t` at any point in time, as

1. This would be super confusing to reviewers, if pointers cannot be accessed via `*data32`
2. This would be undefined behavior (and compilers like to kick your ass, if they detect undefined behavior in your code)

So just change the type to either `void *` (technically the most correct) or to `uint8_t *` for all pointers to `uint32_t` that might be unaligned.

You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210119/4dece13c/attachment.htm>

More information about the notifications mailing list