0

I pack a handful of values inside a 64-bit integer at the end of a network packet. I extract this integer and then use bitwise ops to pull the packed individual values out. This is on 32-bit Android NDK code:

uint64_t footer = *(uint64_t*)(payload + payload_length - 8);
uint32_t v0 = footer & 0xFF; // 8 bits
uint32_t v1 = (footer >> 8) & 0xFFFF; // 16 bits
uint32_t v2 = (footer >> 24) & 0xFFFF; // 16 bits
uint32_t v3 = (footer >> 40) & 0xFFFFFF; // 24 bits

Accessing v2 or v3 seems to cause a crash, as if accessing the tail end of the 64-bit integer is causing a fault.

user1043761
  • 696
  • 6
  • 22
  • "A crash" doesn't help us understand the problem. The exact error text is better. Even better: Drop it in a debugger to find out why. – tadman Aug 10 '20 at 20:32
  • 1
    What does `payload + payload_length - 8` evaluate to? My guess, is 2. – tadman Aug 10 '20 at 20:32
  • `uint64_t footer = *(uint64_t*)(payload + payload_length - 8);` -- Your problems probably start here. That cast looks dubious. Wouldn't be surprised if it's an alignment issue. – PaulMcKenzie Aug 10 '20 at 20:41
  • `(payload + payload_length - 8)` points to the last 8 bytes of the network packet (where the 64-bit integer is). The cast converts the char* (`payload`) into a 64-bit integer pointer, then is dereferenced – user1043761 Aug 10 '20 at 20:59
  • My guess would be Arm not supporting unaligned access. The crash report should tell you if this is the case. – Richard Critten Aug 11 '20 at 00:23

0 Answers0