aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-11-15 10:28:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-11-15 10:28:32 -0300
commit25cd3d377ec13176a6701d9d21a278ba8f2bc3d6 (patch)
treedb4d9d08f79599f0fdfafec166ff4390a195dedc /lauxlib.c
parent1028f296a8e6477cb556c75fe1397cd4e2762abe (diff)
downloadlua-25cd3d377ec13176a6701d9d21a278ba8f2bc3d6.tar.gz
lua-25cd3d377ec13176a6701d9d21a278ba8f2bc3d6.tar.bz2
lua-25cd3d377ec13176a6701d9d21a278ba8f2bc3d6.zip
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.
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 73190975..927e36f0 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1124,29 +1124,30 @@ static void warnfon (void *ud, const char *message, int tocont) {
1124#include <time.h> 1124#include <time.h>
1125 1125
1126 1126
1127/* 1127/* Size for the buffer, in bytes */
1128** Size of 'e' measured in number of 'unsigned int's. (In the weird 1128#define BUFSEEDB (sizeof(void*) + sizeof(time_t))
1129** case that the division truncates, we just lose some part of the 1129
1130** value, no big deal.) 1130/* Size for the buffer in int's, rounded up */
1131*/ 1131#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
1132#define sof(e) (sizeof(e) / sizeof(unsigned int))
1133 1132
1134 1133
1135#define addbuff(b,v) \ 1134#define addbuff(b,v) (memcpy(b, &(v), sizeof(v)), b += sizeof(v))
1136 (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v))
1137 1135
1138 1136
1139static unsigned int luai_makeseed (void) { 1137static unsigned int luai_makeseed (void) {
1140 unsigned int buff[sof(void*) + sof(time_t)]; 1138 unsigned int buff[BUFSEED];
1141 unsigned int res; 1139 unsigned int res;
1142 unsigned int *b = buff; 1140 unsigned int i;
1143 time_t t = time(NULL); 1141 time_t t = time(NULL);
1144 void *h = buff; 1142 void *h = buff;
1143 char *b = (char*)buff;
1145 addbuff(b, h); /* local variable's address */ 1144 addbuff(b, h); /* local variable's address */
1146 addbuff(b, t); /* time */ 1145 addbuff(b, t); /* time */
1146 /* fill (rare but possible) remain of the buffer with zeros */
1147 memset(b, 0, BUFSEED * sizeof(int) - BUFSEEDB);
1147 res = buff[0]; 1148 res = buff[0];
1148 for (b = buff + 1; b < buff + sof(buff); b++) 1149 for (i = 0; i < BUFSEED; i++)
1149 res ^= (res >> 3) + (res << 7) + *b; 1150 res ^= (res >> 3) + (res << 7) + buff[i];
1150 return res; 1151 return res;
1151} 1152}
1152 1153