commit ca715348d35207dc14695b5b20acb3305e7b294d
parent 1ff4b273acc0221fb89b922f16d636adc0ea9116
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Sat, 8 Aug 2015 12:29:43 +0000
daemon.c: MHD_handle_connection(): check timeout value for overflow of W32
Diffstat:
3 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/src/include/platform.h b/src/include/platform.h
@@ -189,6 +189,12 @@ typedef SOCKET MHD_socket;
#define MHD_SOCKET_DEFINED 1
#endif /* MHD_SOCKET_DEFINED */
+#ifndef _WIN32
+typedef time_t _MHD_TIMEVAL_TV_SEC_TYPE;
+#else /* _WIN32 */
+typedef long _MHD_TIMEVAL_TV_SEC_TYPE;
+#endif /* _WIN32 */
+
/* Force don't use pipes on W32 */
#if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES)
#define MHD_DONT_USE_PIPES 1
diff --git a/src/microhttpd/MHD_limits.h b/src/microhttpd/MHD_limits.h
@@ -32,6 +32,9 @@
#include <limits.h>
#endif /* HAVE_LIMITS_H */
+#ifndef LONG_MAX
+#define LONG_MAX ((long) ~(((uint64_t) 1) << (8 * sizeof(long) - 1)))
+#endif /* !OFF_T_MAX */
#ifndef INT32_MAX
#define INT32_MAX ((int32_t)0x7FFFFFFF)
@@ -49,5 +52,18 @@
#define OFF64_T_MAX ((off64_t) ~(((uint64_t) 1) << (8 * sizeof(off64_t) - 1)))
#endif /* _LARGEFILE64_SOURCE && !OFF64_T_MAX */
+#ifndef TIME_T_MAX
+/* Assume that time_t is signed type. */
+/* Even if time_t is unsigned, TIME_T_MAX will be safe limit */
+#define TIME_T_MAX ( (time_t) ~(((uint64_t) 1) << (8 * sizeof(time_t) - 1)) )
+#endif /* !TIME_T_MAX */
+
+#ifndef TIMEVAL_TV_SEC_MAX
+#ifndef _WIN32
+#define TIMEVAL_TV_SEC_MAX TIME_T_MAX
+#else /* _WIN32 */
+#define TIMEVAL_TV_SEC_MAX LONG_MAX
+#endif /* _WIN32 */
+#endif /* !TIMEVAL_TV_SEC_MAX */
#endif /* MHD_LIMITS_H */
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -825,8 +825,18 @@ MHD_handle_connection (void *data)
now = MHD_monotonic_time();
if (now - con->last_activity > timeout)
tv.tv_sec = 0;
- else
- tv.tv_sec = timeout - (now - con->last_activity);
+ else
+ {
+ const time_t seconds_left = timeout - (now - con->last_activity);
+#ifndef _WIN32
+ tv.tv_sec = seconds_left;
+#else /* _WIN32 */
+ if (seconds_left > TIMEVAL_TV_SEC_MAX)
+ tv.tv_sec = TIMEVAL_TV_SEC_MAX;
+ else
+ tv.tv_sec = (_MHD_TIMEVAL_TV_SEC_TYPE)seconds_left;
+#endif /* _WIN32 */
+ }
tv.tv_usec = 0;
tvp = &tv;
}