Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 03 Apr 2026 15:27:27 +0000
From:      Jake Freeland <jfree@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: a5a4a2be69f9 - stable/14 - sys/time: Add saturating sbt conversions
Message-ID:  <69cfdc5f.3f9b9.80896b@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/14 has been updated by jfree:

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

commit a5a4a2be69f9696953afa6eb586c62ed96611c37
Author:     Jake Freeland <jfree@FreeBSD.org>
AuthorDate: 2026-03-20 06:33:20 +0000
Commit:     Jake Freeland <jfree@FreeBSD.org>
CommitDate: 2026-04-03 15:26:56 +0000

    sys/time: Add saturating sbt conversions
    
    When converting from timespec to sbintime, the timespec's 64-bit tv_sec
    component is shifted to the left 32 bits, causing any information in the
    upper 32 bits to be lost.
    
    This data loss during conversion can turn timespecs with very large
    tv_sec counters into sbintimes that represent much smaller time
    durations.
    
    Add tstosbt_sat() and tvtosbt_sat(), which are saturating versions of
    tstosbt and tvtosbt. With these routines, any overflow resulting from
    the conversion is clamped to [-SBT_MAX - 1, SBT_MAX].
    
    Reviewed by:            imp, markj
    Differential Revision:  https://reviews.freebsd.org/D55791
    MFC after:              2 weeks
    
    (cherry picked from commit e3799530b3ba38567f8052b9e107884609fc71ea)
---
 sys/sys/time.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/sys/sys/time.h b/sys/sys/time.h
index 31020bbdfdd8..66fbd2dd41f4 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -354,6 +354,16 @@ tstosbt(struct timespec _ts)
 	return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
 }
 
+static __inline sbintime_t
+tstosbt_sat(struct timespec _ts)
+{
+	if (_ts.tv_sec > SBT_MAX >> 32)
+		return (SBT_MAX);
+	if (_ts.tv_sec < -(SBT_MAX >> 32) - 1)
+		return (-SBT_MAX - 1);
+	return (tstosbt(_ts));
+}
+
 static __inline struct timeval
 sbttotv(sbintime_t _sbt)
 {
@@ -370,6 +380,17 @@ tvtosbt(struct timeval _tv)
 
 	return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
 }
+
+static __inline sbintime_t
+tvtosbt_sat(struct timeval _tv)
+{
+	if (_tv.tv_sec > SBT_MAX >> 32)
+		return (SBT_MAX);
+	if (_tv.tv_sec < -(SBT_MAX >> 32) - 1)
+		return (-SBT_MAX - 1);
+	return (tvtosbt(_tv));
+}
+
 #endif /* __BSD_VISIBLE */
 
 #ifdef _KERNEL


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69cfdc5f.3f9b9.80896b>