>From 4c8309661d861d838e42b2277e0d43b16d62e061 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 10 Mar 2015 09:37:50 -0700
Subject: [PROPOSED PATCH] Avoid integer overflow in mktime
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* localtime.c (time2sub), NEWS: Avoid signed arithmetic overflow.
(Problem reported by J��rg Richter.)
---
 NEWS        |  5 +++++
 localtime.c | 15 ++++-----------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/NEWS b/NEWS
index 5aca1f6..1e74bfd 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,11 @@ Unreleased, experimental changes
     Correct the 1992-2010 DST abbreviation in Volgograd from "MSK" to "MSD".
     (Thanks to Hank W.)
 
+  Changes affecting code
+
+    Fix integer overflow bug in reference 'mktime' implementation.
+    (Problem reported by J��rg Richter.)
+
   Changes affecting commentary
 
     Cite the recent Mexican decree changing Quintana Roo's time zone.
diff --git a/localtime.c b/localtime.c
index f44390a..a2d9aa8 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1783,10 +1783,10 @@ time2sub(struct tm *const tmp,
 	register int			dir;
 	register int			i, j;
 	register int			saved_seconds;
-	register int_fast32_t			li;
+	register int_fast32_t		li;
 	register time_t			lo;
 	register time_t			hi;
-	int_fast32_t				y;
+	int_fast32_t			y;
 	time_t				newt;
 	time_t				t;
 	struct tm			yourtm, mytm;
@@ -1861,15 +1861,8 @@ time2sub(struct tm *const tmp,
 	/*
 	** Do a binary search (this works whatever time_t's type is).
 	*/
-	if (!TYPE_SIGNED(time_t)) {
-		lo = 0;
-		hi = lo - 1;
-	} else {
-		lo = 1;
-		for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
-			lo *= 2;
-		hi = -(lo + 1);
-	}
+	lo = time_t_min;
+	hi = time_t_max;
 	for ( ; ; ) {
 		t = lo / 2 + hi / 2;
 		if (t < lo)
-- 
2.1.0

