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