git: b6e6748b2b4a - stable/13 - sctp: avoid undefined behaviour and cleanup the code.

From: Michael Tuexen <tuexen_at_FreeBSD.org>
Date: Wed, 23 Feb 2022 00:14:43 UTC
The branch stable/13 has been updated by tuexen:

URL: https://cgit.FreeBSD.org/src/commit/?id=b6e6748b2b4aca5b30226213201fdc61caee3cd3

commit b6e6748b2b4aca5b30226213201fdc61caee3cd3
Author:     Michael Tuexen <tuexen@FreeBSD.org>
AuthorDate: 2022-02-17 18:23:59 +0000
Commit:     Michael Tuexen <tuexen@FreeBSD.org>
CommitDate: 2022-02-23 00:13:10 +0000

    sctp: avoid undefined behaviour and cleanup the code.
    
    (cherry picked from commit 76e03cc940fed57d580b1d5c0605e8af2e14f05b)
---
 sys/netinet/sctp_crc32.c | 29 +++++++++++------------------
 1 file 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