Subject: bug#27706: MacOS: decode-time hang

From: [email-masked] (Charles A. Roelli)
Date: Sat, 30 Sep 2017 21:09:08 +0200

> From: "Charles A. Roelli" <[email-masked]>
> Date: Sat, 15 Jul 2017 16:19:39 +0200
> 
> Evaluating the following form on my system (macOS 10.6) results in a
> hang from Emacs 23 to master:
> 
> (decode-time '(-1034058203136 0))
> 
> [other values around -1034058203136 don't cause an issue]
> 
> The stack trace (stuck in macOS' libc, I think):
> 
> [...]
> 
> The form is called when compiling the new org-timer.el, which requires
> org-clock.el, which itself contains this call that causes the issue:
> 
> (defconst org-clock--oldest-date
>    (let* ((dichotomy
> 	  (lambda (min max pred)
> 	    (if (funcall pred min) min
> 	      (cl-incf min)
> 	      (while (> (- max min) 1)
> 		(let ((mean (+ (ash min -1) (ash max -1) (logand min max 1))))
> 		  (if (funcall pred mean) (setq max mean) (setq min mean)))))
> 	    max))
> 	 (high
> 	  (funcall dichotomy
> 		   most-negative-fixnum
> 		   0
> 		   (lambda (m) (ignore-errors (decode-time (list m 0))))))
> 	 (low
> 	  (funcall dichotomy
> 		   most-negative-fixnum
> 		   0
> 		   (lambda (m) (ignore-errors (decode-time (list high m)))))))
>      (list high low))
>    "Internal time for oldest date representable on the system.")
> 
> As a result, "make" hangs for me, so I delete org-timer.el locally to
> get around the problem for now.  But I'd like to find a more permanent
> solution to make sure this doesn't happen elsewhere.
> 
> Can anyone reproduce this under macOS, and if so, under which macOS
> version?  I'd like to know which versions might need a fix for this
> problem.

I'd like to fix this for emacs-26.  Is this fix okay to apply?

>From b7a871e126b6e2036dbbe81b5c8b36bfbf6da419 Mon Sep 17 00:00:00 2001
From: "Charles A. Roelli" <[email-masked]>
Date: Sat, 30 Sep 2017 20:42:03 +0200
Subject: [PATCH] Workaround for faulty localtime() under macOS 10.6

* lisp/org/org-clock.el (org-clock--oldest-date): Only execute
'decode-time' on times later than year -2**31 under macOS 10.6.
See Bug#27706.
---
 lisp/org/org-clock.el | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/org/org-clock.el b/lisp/org/org-clock.el
index 8df185d..97d9612 100644
--- a/lisp/org/org-clock.el
+++ b/lisp/org/org-clock.el
@@ -478,7 +478,18 @@ org-clock--oldest-date
 	  (funcall dichotomy
 		   most-negative-fixnum
 		   0
-		   (lambda (m) (ignore-errors (decode-time (list m 0))))))
+		   (lambda (m)
+                     ;; libc in macOS 10.6 hangs when decoding times
+                     ;; around year -2**31.  Limit `high' not to go
+                     ;; any earlier than that.
+                     (if (and (featurep 'ns)
+                              (string-match-p
+                               "10\\.6\\.[[:digit:]]"
+                               (shell-command-to-string
+                                "sw_vers -productVersion")))
+                         (if (> m -1034058203136)
+                             (ignore-errors (decode-time (list m 0))))
+                       (ignore-errors (decode-time (list m 0)))))))
 	 (low
 	  (funcall dichotomy
 		   most-negative-fixnum
-- 
2.9.4