ref: f83bec968b08889825adb21535e4299eaa21e009
parent: db7bbf53eac7b4e3f7c65dcb22afc7e20eaa1dfc
author: Ralph Giles <[email protected]>
date: Sun Nov 4 17:03:48 EST 2012
Add a native-endian int32 reader. Fixes an ARM warning. Arm (and other) architectures have stricter alignment requirements thank x86, so just casting a char* to an int* and reading from it could cause an exception, and because of this gcc warns on that platform. I'm pretty sure this particular read would always be aligned in pracice, since it's the first word of the network packet returned by pcap, but adding a more general helper is reasonable. We memcpy() the value into a union, which the compiler should allocate aligned, and then read the data back out as an int. Thanks to Ron for reporting the issue, and Greg for suggesting the fix.
--- a/src/opusrtp.c
+++ b/src/opusrtp.c
@@ -331,6 +331,19 @@
return v;
}
+/* helper, read a native-endian 32 bit int from memory */
+static int rne32(const unsigned char *p)
+{
+ /* On x86 we could just cast, but that might not meet
+ * arm's alignment requirements. */
+ union {
+ unsigned char c[4];
+ int d;
+ } m;
+ memcpy(m.c, p, 4);
+ return m.d;
+}
+
int parse_eth_header(const unsigned char *packet, int size, eth_header *eth)
{
if (!packet || !eth) {
@@ -358,7 +371,7 @@
return -1;
}
/* protocol is in host byte order */
- loop->family = *(int*)packet;
+ loop->family = rne32(packet);
return 0;
}