Thread (3 messages) flat view 3 messages, 3 authors, 2016-06-15

Re: [PATCH 1/2] date: recognize bogus FreeBSD gmtime output

From: René Scharfe <hidden>
Date: 2016-06-15 23:00:37

Possibly related (same subject, not in this thread)

Am 01.04.2014 21:08, schrieb Junio C Hamano:
René Scharfe [off-list ref] writes:
quoted
Am 01.04.2014 09:42, schrieb Jeff King:
quoted
diff --git a/compat/gmtime.c b/compat/gmtime.c
new file mode 100644
index 0000000..ffcabf4
--- /dev/null
+++ b/compat/gmtime.c
@@ -0,0 +1,26 @@
+#include "../git-compat-util.h"
+#undef gmtime
+#undef gmtime_r
+
+struct tm *git_gmtime(const time_t *timep)
+{
+	static struct tm result;
+	return git_gmtime_r(timep, &result);
+}
+
+struct tm *git_gmtime_r(const time_t *timep, struct tm *result)
+{
+	struct tm *ret;
+
+	ret = gmtime_r(timep, result);
+
+	/*
+	 * Rather than NULL, FreeBSD gmtime will return a "struct tm" with all
+	 * fields zeroed. Since "mday" cannot otherwise be zero, we can test
+	 * this very quickly.
+	 */
+	if (ret && !ret->tm_mday)
+		ret = NULL;
+
+	return ret;
+}
http://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html
says that errno shall be set on error and only mentions EOVERFLOW as a
possible error code.
So are you saying we should set EOVERFLOW ourselves, or does FreeBSD
set EOVERFLOW for us in this case and we do not have to worry?
If we correct the return value then we should correct errno as well. 
gmtime() on FreeBSD 10 leaves errno untouched when it encounters an 
overflow.

While testing this again I just noticed that FreeBSD doesn't strictly 
return a pointer to a cleared struct tm.  It simply leaves its static 
buffer untouched.  We should probably clear it (memset in git_gmtime_r).

Demo program:

-- >8 --
#include <stdio.h>
#include <time.h>

const static time_t epoch;

int main(int argc, char **argv)
{
         time_t t = 99999999999999999;

         printf("%s", ctime(&t));
         printf("%s", asctime(gmtime(&t)));

         printf("%s", ctime(&t));
         gmtime(&epoch);
         printf("%s", asctime(gmtime(&t)));

         return 0;
}
-- 8< --

Results on FreeBSD 10:

	Wed Sep  6 11:46:39     -1126091476
	Wed Sep  6 11:46:39     -1126091476
	Wed Sep  6 11:46:39     -1126091476
	Thu Jan  1 00:00:00 1970

René
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help