git: e66b677c9745 - stable/12 - kern_ntptime: Fix undefined behavior of the shift operator

From: Cy Schubert <cy_at_FreeBSD.org>
Date: Fri, 23 Jun 2023 04:35:44 UTC
The branch stable/12 has been updated by cy:

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

commit e66b677c974540ccf55d7a73a3ce220721f4d0ea
Author:     Dmitriy Alexandrov <d06alexandrov@users.noreply.github.com>
AuthorDate: 2023-06-08 09:08:46 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2023-06-23 04:35:04 +0000

    kern_ntptime: Fix undefined behavior of the shift operator
    
    L_LINT macro is used with negative numbers [i.e.
    L_LINT(time_freq, -MAXFREQ)], it could cause undefined
    behavior. It should be similar to the L_RSHIFT(v, n) macro.
    
    MFC after:      2 weeks
    Reviewed by:    cy
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/769
    Signed-off-by: Dmitriy Alexandrov <d06alexandrov@gmail.com>
    (cherry picked from commit af9ce4e9bb7d717279e02d46455e85ef6fb828f7)
---
 sys/kern/kern_ntptime.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_ntptime.c b/sys/kern/kern_ntptime.c
index bc35cdee0d71..2817d2f20d5a 100644
--- a/sys/kern/kern_ntptime.c
+++ b/sys/kern/kern_ntptime.c
@@ -73,7 +73,13 @@ typedef int64_t l_fp;
 #define L_MPY(v, a)	((v) *= (a))
 #define L_CLR(v)	((v) = 0)
 #define L_ISNEG(v)	((v) < 0)
-#define L_LINT(v, a)	((v) = (int64_t)(a) << 32)
+#define L_LINT(v, a) \
+	do { \
+		if ((a) < 0) \
+			((v) = -((int64_t)(-(a)) << 32)); \
+		else \
+			((v) = (int64_t)(a) << 32); \
+	} while (0)
 #define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
 
 /*