Skip to content

Commit

Permalink
Fix unalign pointer conversion in Announce message
Browse files Browse the repository at this point in the history
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior wire/Announce.h:281:15 in
wire/Announce.h:281:15: runtime error: load of misaligned address 0x61900000318c for type 'uint64_t' (aka 'unsigned long'), which requires 8 byte alignment
0x61900000318c: note: pointer points here
  00 00 00 00 00 00 00 00  00 00 00 00 04 02 00 14  01 07 00 61 14 45 81 00  00 00 00 00 00 00 00 00
              ^
  • Loading branch information
madafoo committed Sep 30, 2017
1 parent fb788bd commit a066006
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions wire/Announce.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,16 @@ struct Announce_Header
uint8_t snodeIp[16];

// Milliseconds since the epoch when this message was crafted and reset flag
uint64_t timeStampVersionFlags_be;
uint8_t timeStampVersionFlags_be[8];
};
#define Announce_Header_SIZE 120
Assert_compileTime(sizeof(struct Announce_Header) == Announce_Header_SIZE);

static inline int64_t Announce_Header_getTimestamp(struct Announce_Header* hdr)
{
return Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) >> 4;
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
return Endian_bigEndianToHost64(ts_be) >> 4;
}

static inline void Announce_Header_setTimestamp(struct Announce_Header* hdr,
Expand All @@ -250,36 +252,45 @@ static inline void Announce_Header_setTimestamp(struct Announce_Header* hdr,
// It will also fail for negative timestamps.
Assert_true(!(uTime >> 60));

hdr->timeStampVersionFlags_be =
(hdr->timeStampVersionFlags_be & Endian_hostToBigEndian64(0x0f)) |
Endian_hostToBigEndian64(uTime << 4);
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
ts_be = (ts_be & Endian_hostToBigEndian64(0x0f)) | Endian_hostToBigEndian64(uTime << 4);
Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
}

static inline bool Announce_Header_isReset(struct Announce_Header* hdr)
{
return (Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) >> 3) & 1;
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
return (Endian_bigEndianToHost64(ts_be) >> 3) & 1;
}

static inline void Announce_Header_setReset(struct Announce_Header* hdr, bool isReset)
{
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
if (isReset) {
hdr->timeStampVersionFlags_be |= Endian_hostToBigEndian64(1<<3);
ts_be |= Endian_hostToBigEndian64(1<<3);
} else {
hdr->timeStampVersionFlags_be &= ~Endian_hostToBigEndian64(1<<3);
ts_be &= ~Endian_hostToBigEndian64(1<<3);
}
Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
}

static inline int Announce_Header_getVersion(struct Announce_Header* hdr)
{
return Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) & 0x07;
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
return Endian_bigEndianToHost64(ts_be) & 0x07;
}

#define Announce_Header_CURRENT_VERSION 1
static inline void Announce_Header_setVersion(struct Announce_Header* hdr, int version)
{
hdr->timeStampVersionFlags_be =
(hdr->timeStampVersionFlags_be & ~Endian_hostToBigEndian64(0x07)) |
Endian_hostToBigEndian64(version & 0x07);
uint64_t ts_be;
Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
ts_be = (ts_be & ~Endian_hostToBigEndian64(0x07)) | Endian_hostToBigEndian64(version & 0x07);
Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
}

static inline struct Announce_ItemHeader* Announce_ItemHeader_next(struct Message* msg, void* last)
Expand Down

0 comments on commit a066006

Please sign in to comment.