APPROVE COMMIT
NOTE: This patch has been committed
# HG changeset patch
# User Aidan Kehoe <kehoea(a)parhasard.net>
# Date 1494532970 -3600
#      Thu May 11 21:02:50 2017 +0100
# Node ID d745116738120282e98054133b2835e04a206758
# Parent  50b05df76160d68a18c7afcaf4f66c0fb339dcbe
Restrict the integer values passed as the ZONE argument, #'encode-time
src/ChangeLog addition:
2017-05-11  Aidan Kehoe  <kehoea(a)parhasard.net>
	* editfns.c (Fencode_time):
	Restrict the possible integer values passed as the ZONE argument
	to this function, as per the OS X and Linux documentation for
	tzset(3).
tests/ChangeLog addition:
2017-05-11  Aidan Kehoe  <kehoea(a)parhasard.net>
	* automated/os-tests.el:
	Test restrictions just introduced to the integer values passed as
	the ZONE argument to #'encode-time.
diff -r 50b05df76160 -r d74511673812 src/ChangeLog
--- a/src/ChangeLog	Thu May 11 19:58:56 2017 +0100
+++ b/src/ChangeLog	Thu May 11 21:02:50 2017 +0100
@@ -1,3 +1,10 @@
+2017-05-11  Aidan Kehoe  <kehoea(a)parhasard.net>
+
+	* editfns.c (Fencode_time):
+	Restrict the possible integer values passed as the ZONE argument
+	to this function, as per the OS X and Linux documentation for
+	tzset(3).
+
 2017-05-11  Aidan Kehoe  <kehoea(a)parhasard.net>
 
 	* alloc.c (resize_string):
diff -r 50b05df76160 -r d74511673812 src/editfns.c
--- a/src/editfns.c	Thu May 11 19:58:56 2017 +0100
+++ b/src/editfns.c	Thu May 11 21:02:50 2017 +0100
@@ -1230,24 +1230,40 @@
       /* #### This business of modifying environ is horrendous!
 	 Why don't we just putenv()?  Why don't we implement our own
 	 funs that don't require this futzing? */
-      Extbyte tzbuf[100];
+      Ibyte tzbuf[100];
       Extbyte *tzstring;
       Extbyte **oldenv = environ, **newenv;
 
       if (STRINGP (zone))
 	tzstring = LISP_STRING_TO_EXTERNAL (zone, Qtime_zone_encoding);
-      else if (FIXNUMP (zone))
+      else if (INTEGERP (zone))
 	{
-	  int abszone = abs (XFIXNUM (zone));
-	  /* We specify the time zone in offset notation (see `man
-	     tzset' for details).  The offset indicates the value one
-	     must add to local time to arrive at UTC.  Thus, we sign
-	     the offset with a `-' if the time zone is east of GMT; we
-	     sign the offset with a `+' if the time zone is GMT (then
-	     the offset is 0) or if the time zone is west of GMT. */
-	  sprintf (tzbuf, "XXX%s%d:%02d:%02d", (XFIXNUM (zone) < 0) ? "+"
: "-",
-		   abszone / (60*60), (abszone/60) % 60, abszone % 60);
-	  tzstring = tzbuf;
+          int abszone;
+          Bytecount bufwritten;
+
+          /* The number of hours must be less or equal to 24, documented as
+             such on OS X and Linux as of 2017. */
+          check_integer_range (zone, make_fixnum (-86400),
+                               make_fixnum (86400));
+
+          abszone = abs ((int) (XFIXNUM (zone)));
+
+          /* We specify the time zone in offset notation (see `man tzset' for
+             details).  The offset indicates the value one must add to local
+             time to arrive at UTC; the Emacs sign convention is the opposite
+             of that used by tzset(3).
+
+             Thus, we sign the offset with a `-' if the time zone is east of
+             GMT; we sign the offset with a `+' if the time zone is GMT (then
+             the offset is 0) or if the time zone is west of GMT. */
+          bufwritten
+            = emacs_snprintf (tzbuf, sizeof (tzbuf), "XXX%s%d:%02d:%02d", 
+                              XFIXNUM (zone) <= 0 ? "+" : "-",
+                              abszone / (60*60),
+                              (abszone/60) % 60,
+                              abszone % 60);
+          assert (bufwritten < (Bytecount) (sizeof (tzbuf)));
+          tzstring = (Extbyte *) tzbuf;
 	}
       else
 	invalid_argument ("Invalid time zone specification", Qunbound);
diff -r 50b05df76160 -r d74511673812 tests/ChangeLog
--- a/tests/ChangeLog	Thu May 11 19:58:56 2017 +0100
+++ b/tests/ChangeLog	Thu May 11 21:02:50 2017 +0100
@@ -1,3 +1,9 @@
+2017-05-11  Aidan Kehoe  <kehoea(a)parhasard.net>
+
+	* automated/os-tests.el:
+	Test restrictions just introduced to the integer values passed as
+	the ZONE argument to #'encode-time.
+
 2017-03-16  Aidan Kehoe  <kehoea(a)parhasard.net>
 
 	* automated/lisp-reader-tests.el (args-out-of-range):
diff -r 50b05df76160 -r d74511673812 tests/automated/os-tests.el
--- a/tests/automated/os-tests.el	Thu May 11 19:58:56 2017 +0100
+++ b/tests/automated/os-tests.el	Thu May 11 21:02:50 2017 +0100
@@ -123,4 +123,13 @@
   do 
   (Assert (string= post (substitute-in-file-name pre))))
 
+;; Check some restrictions introduced to the ZONE argument to #'encode-time.
+
+(Check-Error (encode-time 24 4 20 11 5 2017 -86401) args-out-of-range)
+(Assert (equal (encode-time 24 4 20 11 5 2017 -86400)
+               '(22806 . 5448))) ;; "05/12/17 09:04:25 PM"
+(Assert (equal (encode-time 24 4 20 11 5 2017 86400)
+               '(22803 . 29256))) ;; "05/10/17 09:04:24 PM"
+(Check-Error (encode-time 24 4 20 11 5 2017 86401) args-out-of-range)
+
 ;;; end of os-tests.el
-- 
‘As I sat looking up at the Guinness ad, I could never figure out /
How your man stayed up on the surfboard after forty pints of stout’
(C. Moore)
    
    
    
 
                    
                    
                        
                        Show replies by date