]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
sctp: Fix undefined behavior in left shift operation
authorYu-Chun Lin <eleanor15x@gmail.com>
Tue, 18 Feb 2025 08:12:16 +0000 (16:12 +0800)
committerJakub Kicinski <kuba@kernel.org>
Thu, 20 Feb 2025 02:55:40 +0000 (18:55 -0800)
According to the C11 standard (ISO/IEC 9899:2011, 6.5.7):
"If E1 has a signed type and E1 x 2^E2 is not representable in the result
type, the behavior is undefined."

Shifting 1 << 31 causes signed integer overflow, which leads to undefined
behavior.

Fix this by explicitly using '1U << 31' to ensure the shift operates on
an unsigned type, avoiding undefined behavior.

Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com>
Link: https://patch.msgid.link/20250218081217.3468369-1-eleanor15x@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/sctp/stream.c

index c241cc552e8d584d72e5d657f1d6479065b6b164..bfcff6d6a438660f9f0ad565011ccc2516c3a8d7 100644 (file)
@@ -735,7 +735,7 @@ struct sctp_chunk *sctp_process_strreset_tsnreq(
         *     value SHOULD be the smallest TSN not acknowledged by the
         *     receiver of the request plus 2^31.
         */
-       init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
+       init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1U << 31);
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
                         init_tsn, GFP_ATOMIC);