aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2012-02-01 19:57:15 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2012-02-01 19:57:15 -0200
commit678c1255c92eed9c2c7564d340a5563b17395158 (patch)
treeca35d119d770835b59d34cdec93805190d3155d1 /lstring.c
parenta4b96ce9a3305ae3585c0bb143fa7342c140f20b (diff)
downloadlua-678c1255c92eed9c2c7564d340a5563b17395158.tar.gz
lua-678c1255c92eed9c2c7564d340a5563b17395158.tar.bz2
lua-678c1255c92eed9c2c7564d340a5563b17395158.zip
random seed used in the hash of all strings to avoid intentional
collisions
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/lstring.c b/lstring.c
index 75861cf4..a704af4e 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 roberto Exp roberto $ 2** $Id: lstring.c,v 2.21 2012/01/25 21:05:40 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -37,8 +37,8 @@ int luaS_eqstr (TString *a, TString *b) {
37} 37}
38 38
39 39
40unsigned int luaS_hash (const char *str, size_t l) { 40unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
41 unsigned int h = cast(unsigned int, l); /* seed */ 41 unsigned int h = seed ^ l;
42 size_t l1; 42 size_t l1;
43 for (l1 = 0; l1 < l; l1++) 43 for (l1 = 0; l1 < l; l1++)
44 h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1])); 44 h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1]));
@@ -120,8 +120,9 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l,
120*/ 120*/
121static TString *internshrstr (lua_State *L, const char *str, size_t l) { 121static TString *internshrstr (lua_State *L, const char *str, size_t l) {
122 GCObject *o; 122 GCObject *o;
123 unsigned int h = luaS_hash(str, l); 123 global_State *g = G(L);
124 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 124 unsigned int h = luaS_hash(str, l, g->seed);
125 for (o = g->strt.hash[lmod(h, g->strt.size)];
125 o != NULL; 126 o != NULL;
126 o = gch(o)->next) { 127 o = gch(o)->next) {
127 TString *ts = rawgco2ts(o); 128 TString *ts = rawgco2ts(o);
@@ -146,7 +147,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
146 else { 147 else {
147 if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 148 if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
148 luaM_toobig(L); 149 luaM_toobig(L);
149 return createstrobj(L, str, l, LUA_TLNGSTR, 0, NULL); 150 return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
150 } 151 }
151} 152}
152 153