aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 4ca6c654..555a5976 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1091,8 +1091,54 @@ static void warnfon (void *ud, const char *message, int tocont) {
1091} 1091}
1092 1092
1093 1093
1094
1095/*
1096** A function to compute an unsigned int with some level of
1097** randomness. Rely on Address Space Layout Randomization (if present)
1098** and the current time.
1099*/
1100#if !defined(luai_makeseed)
1101
1102#include <time.h>
1103
1104
1105/*
1106** Size of 'e' measured in number of 'unsigned int's. (In the weird
1107** case that the division truncates, we just lose some part of the
1108** value, no big deal.)
1109*/
1110#define sof(e) (sizeof(e) / sizeof(unsigned int))
1111
1112
1113#define addbuff(b,v) \
1114 (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v))
1115
1116
1117static unsigned int luai_makeseed (void) {
1118 unsigned int buff[sof(void*) + sof(time_t)];
1119 unsigned int res;
1120 unsigned int *b = buff;
1121 time_t t = time(NULL);
1122 void *h = buff;
1123 addbuff(b, h); /* local variable's address */
1124 addbuff(b, t); /* time */
1125 res = buff[0];
1126 for (b = buff + 1; b < buff + sof(buff); b++)
1127 res ^= (res >> 3) + (res << 7) + *b;
1128 return res;
1129}
1130
1131#endif
1132
1133
1134LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1135 (void)L; /* unused */
1136 return luai_makeseed();
1137}
1138
1139
1094LUALIB_API lua_State *luaL_newstate (void) { 1140LUALIB_API lua_State *luaL_newstate (void) {
1095 lua_State *L = lua_newstate(l_alloc, NULL); 1141 lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
1096 if (l_likely(L)) { 1142 if (l_likely(L)) {
1097 lua_atpanic(L, &panic); 1143 lua_atpanic(L, &panic);
1098 lua_setwarnf(L, warnfoff, L); /* default is warnings off */ 1144 lua_setwarnf(L, warnfoff, L); /* default is warnings off */