[riot-notifications] [RIOT-OS/RIOT] sys: add unaligned.h (header for alignment safe value-from-pointer functions) (#10797)

Juan I Carrano notifications at github.com
Thu Jan 17 15:01:05 CET 2019


jcarrano requested changes on this pull request.

If the prototype of `fletcher32()` is `uint32_t fletcher32(const uint16_t *, size_t )` then it is not OK to call it with an unaligned pointer.

> +
+/** @brief Unaligned access helper struct (uint16_t version) */
+typedef struct {
+    uint16_t val;       /**< value */
+} __attribute__((packed)) uint16_una_t;
+
+/**
+ * @brief    Get uint16_t from possibly unaligned pointer
+ *
+ * @param[in]   ptr pointer to read from
+ *
+ * @returns value read from @p ptr
+ */
+static inline uint16_t get_unaligned_u16(const uint16_t *ptr)
+{
+    const uint16_una_t *tmp = (const uint16_una_t *)ptr;

This is discarding a `const` qualifier.

> +extern "C" {
+#endif
+
+/** @brief Unaligned access helper struct (uint16_t version) */
+typedef struct {
+    uint16_t val;       /**< value */
+} __attribute__((packed)) uint16_una_t;
+
+/**
+ * @brief    Get uint16_t from possibly unaligned pointer
+ *
+ * @param[in]   ptr pointer to read from
+ *
+ * @returns value read from @p ptr
+ */
+static inline uint16_t get_unaligned_u16(const uint16_t *ptr)

If this function takes a `const uint16_t *` then it is NOT ok to call it with an unaligned pointer. This should either take a `void *` or be turned into a macro.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/10797#pullrequestreview-193634656
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190117/fc30c9a5/attachment.html>


More information about the notifications mailing list