Blame SOURCES/Add-timestamp-helper-functions.patch

e58a44
From 6437685130b68670888db1d0551f5464d56c4cec Mon Sep 17 00:00:00 2001
e58a44
From: Greg Hudson <ghudson@mit.edu>
e58a44
Date: Sat, 22 Apr 2017 09:49:12 -0400
e58a44
Subject: [PATCH] Add timestamp helper functions
e58a44
e58a44
Add k5-int.h helper functions to manipulate krb5_timestamp values,
e58a44
avoiding undefined behavior and treating negative timestamp values as
e58a44
times between 2038 and 2106.  Add a doxygen comment for krb5_timestamp
e58a44
indicating how third-party code should use it safely.
e58a44
e58a44
ticket: 8352
e58a44
(cherry picked from commit 58e9155060cd93b1a7557e37fbc9b077b76465c2)
e58a44
---
e58a44
 src/include/k5-int.h      | 31 +++++++++++++++++++++++++++++++
e58a44
 src/include/krb5/krb5.hin |  9 +++++++++
e58a44
 2 files changed, 40 insertions(+)
e58a44
e58a44
diff --git a/src/include/k5-int.h b/src/include/k5-int.h
e58a44
index 06ca2b66d..82ee20760 100644
e58a44
--- a/src/include/k5-int.h
e58a44
+++ b/src/include/k5-int.h
e58a44
@@ -2353,6 +2353,37 @@ k5memdup0(const void *in, size_t len, krb5_error_code *code)
e58a44
     return ptr;
e58a44
 }
e58a44
 
e58a44
+/* Convert a krb5_timestamp to a time_t value, treating the negative range of
e58a44
+ * krb5_timestamp as times between 2038 and 2106 (if time_t is 64-bit). */
e58a44
+static inline time_t
e58a44
+ts2tt(krb5_timestamp timestamp)
e58a44
+{
e58a44
+    return (time_t)(uint32_t)timestamp;
e58a44
+}
e58a44
+
e58a44
+/* Return the delta between two timestamps (a - b) as a signed 32-bit value,
e58a44
+ * without relying on undefined behavior. */
e58a44
+static inline krb5_deltat
e58a44
+ts_delta(krb5_timestamp a, krb5_timestamp b)
e58a44
+{
e58a44
+    return (krb5_deltat)((uint32_t)a - (uint32_t)b);
e58a44
+}
e58a44
+
e58a44
+/* Increment a timestamp by a signed 32-bit interval, without relying on
e58a44
+ * undefined behavior. */
e58a44
+static inline krb5_timestamp
e58a44
+ts_incr(krb5_timestamp ts, krb5_deltat delta)
e58a44
+{
e58a44
+    return (krb5_timestamp)((uint32_t)ts + (uint32_t)delta);
e58a44
+}
e58a44
+
e58a44
+/* Return true if a comes after b. */
e58a44
+static inline krb5_boolean
e58a44
+ts_after(krb5_timestamp a, krb5_timestamp b)
e58a44
+{
e58a44
+    return (uint32_t)a > (uint32_t)b;
e58a44
+}
e58a44
+
e58a44
 krb5_error_code KRB5_CALLCONV
e58a44
 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
e58a44
                               krb5_ccache ccache,
e58a44
diff --git a/src/include/krb5/krb5.hin b/src/include/krb5/krb5.hin
e58a44
index cf60d6c41..53ad85384 100644
e58a44
--- a/src/include/krb5/krb5.hin
e58a44
+++ b/src/include/krb5/krb5.hin
e58a44
@@ -187,7 +187,16 @@ typedef krb5_int32 krb5_cryptotype;
e58a44
 
e58a44
 typedef krb5_int32      krb5_preauthtype; /* This may change, later on */
e58a44
 typedef krb5_int32      krb5_flags;
e58a44
+
e58a44
+/**
e58a44
+ * Represents a timestamp in seconds since the POSIX epoch.  This legacy type
e58a44
+ * is used frequently in the ABI, but cannot represent timestamps after 2038 as
e58a44
+ * a positive number.  Code which uses this type should cast values of it to
e58a44
+ * uint32_t so that negative values are treated as timestamps between 2038 and
e58a44
+ * 2106 on platforms with 64-bit time_t.
e58a44
+ */
e58a44
 typedef krb5_int32      krb5_timestamp;
e58a44
+
e58a44
 typedef krb5_int32      krb5_deltat;
e58a44
 
e58a44
 /**