diff options
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 50 |
1 files changed, 49 insertions, 1 deletions
@@ -1091,8 +1091,56 @@ 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 | ** current time, and clock. | ||
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 | |||
1117 | static unsigned int luai_makeseed (void) { | ||
1118 | unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)]; | ||
1119 | unsigned int res; | ||
1120 | unsigned int *b = buff; | ||
1121 | clock_t c = clock(); | ||
1122 | time_t t = time(NULL); | ||
1123 | void *h = buff; | ||
1124 | addbuff(b, h); /* local variable's address */ | ||
1125 | addbuff(b, c); /* clock */ | ||
1126 | addbuff(b, t); /* time */ | ||
1127 | res = buff[0]; | ||
1128 | for (b = buff + 1; b < buff + sof(buff); b++) | ||
1129 | res += *b; | ||
1130 | return res; | ||
1131 | } | ||
1132 | |||
1133 | #endif | ||
1134 | |||
1135 | |||
1136 | LUALIB_API unsigned int luaL_makeseed (lua_State *L) { | ||
1137 | (void)L; /* unused */ | ||
1138 | return luai_makeseed(); | ||
1139 | } | ||
1140 | |||
1141 | |||
1094 | LUALIB_API lua_State *luaL_newstate (void) { | 1142 | LUALIB_API lua_State *luaL_newstate (void) { |
1095 | lua_State *L = lua_newstate(l_alloc, NULL); | 1143 | lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); |
1096 | if (l_likely(L)) { | 1144 | if (l_likely(L)) { |
1097 | lua_atpanic(L, &panic); | 1145 | lua_atpanic(L, &panic); |
1098 | lua_setwarnf(L, warnfoff, L); /* default is warnings off */ | 1146 | lua_setwarnf(L, warnfoff, L); /* default is warnings off */ |