Skip to content

Commit

Permalink
NNI_GETXX macros are brittle due to casting mistake.
Browse files Browse the repository at this point in the history
Essentially, we forgot to dereference as uint8_t *, which can
lead to some suprises if these are misused.
  • Loading branch information
gdamore committed Nov 2, 2024
1 parent b3936a2 commit 9b27984
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/core/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,25 @@ typedef void (*nni_cb)(void *);
(ptr)[7] = (uint8_t) ((uint64_t) (u)); \
} while (0)

#define NNI_GET16(ptr, v) \
v = (((uint16_t) ((uint8_t) (ptr)[0])) << 8u) + \
(((uint16_t) (uint8_t) (ptr)[1]))

#define NNI_GET32(ptr, v) \
v = (((uint32_t) ((uint8_t) (ptr)[0])) << 24u) + \
(((uint32_t) ((uint8_t) (ptr)[1])) << 16u) + \
(((uint32_t) ((uint8_t) (ptr)[2])) << 8u) + \
(((uint32_t) (uint8_t) (ptr)[3]))

#define NNI_GET64(ptr, v) \
v = (((uint64_t) ((uint8_t) (ptr)[0])) << 56u) + \
(((uint64_t) ((uint8_t) (ptr)[1])) << 48u) + \
(((uint64_t) ((uint8_t) (ptr)[2])) << 40u) + \
(((uint64_t) ((uint8_t) (ptr)[3])) << 32u) + \
(((uint64_t) ((uint8_t) (ptr)[4])) << 24u) + \
(((uint64_t) ((uint8_t) (ptr)[5])) << 16u) + \
(((uint64_t) ((uint8_t) (ptr)[6])) << 8u) + \
(((uint64_t) (uint8_t) (ptr)[7]))
#define NNI_GET16(ptr, v) \
v = (((uint16_t) (((uint8_t *) (ptr))[0])) << 8u) + \
((uint16_t) ((uint8_t *) (ptr))[1])

#define NNI_GET32(ptr, v) \
v = (((uint32_t) ((uint8_t *) (ptr))[0]) << 24u) + \
(((uint32_t) ((uint8_t *) (ptr))[1]) << 16u) + \
(((uint32_t) ((uint8_t *) (ptr))[2]) << 8u) + \
((uint32_t) ((uint8_t *) (ptr))[3])

#define NNI_GET64(ptr, v) \
v = (((uint64_t) ((uint8_t *) (ptr))[0]) << 56u) + \
(((uint64_t) ((uint8_t *) (ptr))[1]) << 48u) + \
(((uint64_t) ((uint8_t *) (ptr))[2]) << 40u) + \
(((uint64_t) ((uint8_t *) (ptr))[3]) << 32u) + \
(((uint64_t) ((uint8_t *) (ptr))[4]) << 24u) + \
(((uint64_t) ((uint8_t *) (ptr))[5]) << 16u) + \
(((uint64_t) ((uint8_t *) (ptr))[6]) << 8u) + \
((uint64_t) ((uint8_t *) (ptr))[7])

// Modern CPUs are all little endian. Let's stop paying the endian tax.

Expand Down

0 comments on commit 9b27984

Please sign in to comment.