Buffer in 'luai_makeseed' measured in bytes

In the (rare) cases when sizeof(void*) or sizeof(time_t) are not
multiples of sizeof(int), we still can use all their bytes in the seed.
This commit is contained in:
Roberto Ierusalimschy
2023-11-15 10:28:32 -03:00
parent 1028f296a8
commit 25cd3d377e
2 changed files with 20 additions and 12 deletions

View File

@@ -1124,29 +1124,30 @@ static void warnfon (void *ud, const char *message, int tocont) {
#include <time.h> #include <time.h>
/* /* Size for the buffer, in bytes */
** Size of 'e' measured in number of 'unsigned int's. (In the weird #define BUFSEEDB (sizeof(void*) + sizeof(time_t))
** case that the division truncates, we just lose some part of the
** value, no big deal.) /* Size for the buffer in int's, rounded up */
*/ #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
#define sof(e) (sizeof(e) / sizeof(unsigned int))
#define addbuff(b,v) \ #define addbuff(b,v) (memcpy(b, &(v), sizeof(v)), b += sizeof(v))
(memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v))
static unsigned int luai_makeseed (void) { static unsigned int luai_makeseed (void) {
unsigned int buff[sof(void*) + sof(time_t)]; unsigned int buff[BUFSEED];
unsigned int res; unsigned int res;
unsigned int *b = buff; unsigned int i;
time_t t = time(NULL); time_t t = time(NULL);
void *h = buff; void *h = buff;
char *b = (char*)buff;
addbuff(b, h); /* local variable's address */ addbuff(b, h); /* local variable's address */
addbuff(b, t); /* time */ addbuff(b, t); /* time */
/* fill (rare but possible) remain of the buffer with zeros */
memset(b, 0, BUFSEED * sizeof(int) - BUFSEEDB);
res = buff[0]; res = buff[0];
for (b = buff + 1; b < buff + sof(buff); b++) for (i = 0; i < BUFSEED; i++)
res ^= (res >> 3) + (res << 7) + *b; res ^= (res >> 3) + (res << 7) + buff[i];
return res; return res;
} }

View File

@@ -1160,6 +1160,12 @@ static int num2int (lua_State *L) {
} }
static int makeseed (lua_State *L) {
lua_pushinteger(L, luaL_makeseed(L));
return 1;
}
static int newstate (lua_State *L) { static int newstate (lua_State *L) {
void *ud; void *ud;
lua_Alloc f = lua_getallocf(L, &ud); lua_Alloc f = lua_getallocf(L, &ud);
@@ -1962,6 +1968,7 @@ static const struct luaL_Reg tests_funcs[] = {
{"newstate", newstate}, {"newstate", newstate},
{"newuserdata", newuserdata}, {"newuserdata", newuserdata},
{"num2int", num2int}, {"num2int", num2int},
{"makeseed", makeseed},
{"pushuserdata", pushuserdata}, {"pushuserdata", pushuserdata},
{"querystr", string_query}, {"querystr", string_query},
{"querytab", table_query}, {"querytab", table_query},