git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/5] date: check date overflow against time_t

From
Jeff King <peff@peff.net>
Date
Feb 24, 2014, 07:39 UTC
Message-ID
<20140224073945.GC9969@sigill.intra.peff.net>
In-Reply-To
<20140224073348.GA20221@sigill.intra.peff.net>

When we check whether a timestamp has overflowed, we check only against ULONG_MAX, meaning that strtoul has overflowed. However, we also feed these timestamps to system functions like gmtime, which expect a time_t. On many systems, time_t is actually smaller than "unsigned long" (e.g., because it is signed), and we would overflow when using these functions. We don't know the actual size or signedness of time_t, but we can easily check for truncation with a simple assignment.

Signed-off-by: Jeff King <peff@peff.net>
---
 cache.h |  1 +
 date.c  | 17 +++++++++++++++++
 fsck.c  |  2 +-
 3 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/cache.h b/cache.h
index dc040fb..e405598 100644
--- a/cache.h
+++ b/cache.h
@@ -959,6 +959,7 @@ void datestamp(char *buf, int bufsize);
 unsigned long approxidate_careful(const char *, int *);
 unsigned long approxidate_relative(const char *date, const struct timeval *now);
 enum date_mode parse_date_format(const char *format);
+int date_overflows(unsigned long date);
 
 #define IDENT_STRICT	       1
 #define IDENT_NO_DATE	       2
diff --git a/date.c b/date.c
index 83b4166..90b28f7 100644
--- a/date.c
+++ b/date.c
@@ -1113,3 +1113,20 @@ unsigned long approxidate_careful(const char *date, int *error_ret)
 	gettimeofday(&tv, NULL);
 	return approxidate_str(date, &tv, error_ret);
 }
+
+int date_overflows(unsigned long t)
+{
+	time_t sys;
+
+	/* If we overflowed our unsigned long, that's bad... */
+	if (t == ULONG_MAX)
+		return 1;
+
+	/*
+	 * ...but we also are going to feed the result to system
+	 * functions that expect time_t, which is often "signed long".
+	 * Make sure that we fit into time_t, as well.
+	 */
+	sys = t;
+	return t != sys || (t < 1) != (sys < 1);
+}
diff --git a/fsck.c b/fsck.c
index 760e072..64bf279 100644
--- a/fsck.c
+++ b/fsck.c
@@ -266,7 +266,7 @@ static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
 	(*ident)++;
 	if (**ident == '0' && (*ident)[1] != ' ')
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
-	if (strtoul(*ident, &end, 10) == ULONG_MAX)
+	if (date_overflows(strtoul(*ident, &end, 10)))
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
 	if (end == *ident || *end != ' ')
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
-- 
1.8.5.2.500.g8060133
Previous: Jeff KingNext: Jeff King
Message 4 of 48 in “handle bogus commit dates”
  1. 0/5 handle bogus commit datesJeff King, Feb 24, 2014
  2. 1/5 t4212: test bogus timestamps with git-logJeff King, Feb 24, 2014
  3. 2/5 fsck: report integer overflow in author timestampsJeff King, Feb 24, 2014
  4. 3/5 date: check date overflow against time_tJeff King, Feb 24, 2014
  5. 4/5 log: handle integer overflow in timestampsJeff King, Feb 24, 2014
  6. Junio C HamanoFeb 24, 2014
  7. Jeff KingFeb 24, 2014
  8. Junio C HamanoFeb 24, 2014
  9. Jeff KingFeb 24, 2014
  10. Junio C HamanoFeb 24, 2014
  11. 5/5 log: do not segfault on gmtime errorsJeff King, Feb 24, 2014
  12. René ScharfeMar 22, 2014
  13. Jeff KingMar 24, 2014
  14. René ScharfeMar 24, 2014
  15. Jeff KingMar 24, 2014
  16. Charles BaileyMar 26, 2014
  17. Jeff KingMar 26, 2014
  18. t4212: handle systems with post-apocalyptic gmtimeJeff King, Mar 26, 2014
  19. Junio C HamanoMar 26, 2014
  20. Jeff KingMar 26, 2014
  21. Jeff KingMar 26, 2014
  22. Jeff KingMar 26, 2014
  23. Charles BaileyMar 26, 2014
  24. Jeff KingMar 26, 2014
  25. Charles BaileyMar 26, 2014
  26. Charles BaileyMar 26, 2014
  27. Jeff KingMar 26, 2014
  28. Charles BaileyMar 26, 2014
  29. Jeff KingMar 27, 2014
  30. Junio C HamanoMar 28, 2014
  31. Jeff KingMar 28, 2014
  32. Junio C HamanoMar 28, 2014
  33. Jeff KingMar 28, 2014
  34. Junio C HamanoMar 28, 2014
  35. Jeff KingApr 1, 2014
  36. 1/2 date: recognize bogus FreeBSD gmtime outputJeff King, Apr 1, 2014
  37. René ScharfeApr 1, 2014
  38. Junio C HamanoApr 1, 2014
  39. René ScharfeApr 1, 2014
  40. Jeff KingApr 1, 2014
  41. 2/2 t4212: loosen far-in-future test for AIXJeff King, Apr 1, 2014
  42. work around unreliable gmtime errors on AIXJeff King, Apr 1, 2014
  43. Junio C HamanoApr 1, 2014
  44. Jeff KingApr 1, 2014
  45. Junio C HamanoMar 26, 2014
  46. Jeff KingMar 26, 2014
  47. Junio C HamanoMar 26, 2014
  48. Jeff KingMar 26, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.