From 5a04f1851e0d42b4bcbb0af103490bc964e985aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 20 Mar 2023 16:13:17 -0300 Subject: New function 'luaL_makeseed' This function unifies code from 'lua_newstate', 'math.randomseed', and 'table.sort' that tries to create a value with a minimum level of randomness. --- lstate.c | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) (limited to 'lstate.c') diff --git a/lstate.c b/lstate.c index bee3bf66..1ce6b9a1 100644 --- a/lstate.c +++ b/lstate.c @@ -51,37 +51,6 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) -/* -** A macro to create a "random" seed when a state is created; -** the seed is used to randomize string hashes. -*/ -#if !defined(luai_makeseed) - -#include - -/* -** Compute an initial seed with some level of randomness. -** Rely on Address Space Layout Randomization (if present) and -** current time. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast_sizet(e); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int luai_makeseed (lua_State *L) { - char buff[3 * sizeof(size_t)]; - unsigned int h = cast_uint(time(NULL)); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - -#endif - - /* ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) ** invariant (and avoiding underflows in 'totalobjs') @@ -350,7 +319,7 @@ LUA_API int lua_resetthread (lua_State *L, lua_State *from) { } -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { int i; lua_State *L; global_State *g; @@ -370,7 +339,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->warnf = NULL; g->ud_warn = NULL; g->mainthread = L; - g->seed = luai_makeseed(L); + g->seed = seed; g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; -- cgit v1.2.3-55-g6feb