aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Tuexen <tuexen@FreeBSD.org>2022-02-17 18:23:59 +0000
committerMichael Tuexen <tuexen@FreeBSD.org>2022-02-23 00:13:10 +0000
commitb6e6748b2b4aca5b30226213201fdc61caee3cd3 (patch)
tree82056ff35517ca7ae64640ba8a4dbf4920a356a9
parent8147d64bf18166bd4868821e8492d8bc3e8daa1d (diff)
downloadsrc-b6e6748b2b4aca5b30226213201fdc61caee3cd3.tar.gz
src-b6e6748b2b4aca5b30226213201fdc61caee3cd3.zip
sctp: avoid undefined behaviour and cleanup the code.
(cherry picked from commit 76e03cc940fed57d580b1d5c0605e8af2e14f05b)
-rw-r--r--sys/netinet/sctp_crc32.c29
1 files changed, 11 insertions, 18 deletions
diff --git a/sys/netinet/sctp_crc32.c b/sys/netinet/sctp_crc32.c
index 97b881bb5062..9e17637d74d6 100644
--- a/sys/netinet/sctp_crc32.c
+++ b/sys/netinet/sctp_crc32.c
@@ -52,32 +52,25 @@ __FBSDID("$FreeBSD$");
static uint32_t
sctp_finalize_crc32c(uint32_t crc32c)
{
- uint32_t result;
#if BYTE_ORDER == BIG_ENDIAN
- uint8_t byte0, byte1, byte2, byte3;
+ uint32_t byte0, byte1, byte2, byte3;
#endif
- /* Complement the result */
- result = ~crc32c;
#if BYTE_ORDER == BIG_ENDIAN
/*
- * For BIG-ENDIAN platforms the result is in little-endian form. So
- * we must swap the bytes to return the result in network byte
- * order.
+ * For BIG-ENDIAN platforms, the result is in LITTLE-ENDIAN byte
+ * order. For LITTLE-ENDIAN platforms, the result is in in
+ * BIG-ENDIAN byte order. So for BIG-ENDIAN platforms the bytes must
+ * be swapped to return the result always in network byte order (aka
+ * BIG-ENDIAN).
*/
- byte0 = result & 0x000000ff;
- byte1 = (result >> 8) & 0x000000ff;
- byte2 = (result >> 16) & 0x000000ff;
- byte3 = (result >> 24) & 0x000000ff;
+ byte0 = crc32c & 0x000000ff;
+ byte1 = (crc32c >> 8) & 0x000000ff;
+ byte2 = (crc32c >> 16) & 0x000000ff;
+ byte3 = (crc32c >> 24) & 0x000000ff;
crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
-#else
- /*
- * For LITTLE ENDIAN platforms the result is in already in network
- * byte order.
- */
- crc32c = result;
#endif
- return (crc32c);
+ return (~crc32c);
}
static int