From 7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Nov 2015 14:54:32 -0200 Subject: [PATCH] handling 'clock_t' and 'time_t' correctly in ISO C point of view --- ltablib.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ltablib.c b/ltablib.c index 55a12c7a..ef39d5df 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.87 2015/11/23 11:09:27 roberto Exp roberto $ +** $Id: ltablib.c,v 1.86 2015/11/20 12:30:20 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -12,6 +12,7 @@ #include #include +#include #include "lua.h" @@ -298,14 +299,22 @@ static unsigned int partition (lua_State *L, unsigned int lo, */ #if !defined(l_sortpivot) /* Use 'time' and 'clock' as sources of "randomness" */ - #include +#define szi (sizeof(unsigned int)) +#define sof(e) (sizeof(e)/szi) + static unsigned int choosePivot (unsigned int lo, unsigned int up) { - unsigned int t = (unsigned int)(unsigned long)time(NULL); /* time */ - unsigned int c = (unsigned int)(unsigned long)clock(); /* clock */ + clock_t c = clock(); + time_t t = time(NULL); + unsigned int buff[sof(c) + sof(t)]; unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ - unsigned int p = (c + t) % (r4 * 2) + (lo + r4); + unsigned int p, i, h = 0; + memcpy(buff, &c, sof(c) * szi); + memcpy(buff + sof(c), &t, sof(t) * szi); + for (i = 0; i < sof(buff); i++) + h += buff[i]; + p = h % (r4 * 2) + (lo + r4); lua_assert(lo + r4 <= p && p <= up - r4); return p; }