diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-02-01 19:57:15 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-02-01 19:57:15 -0200 |
commit | 678c1255c92eed9c2c7564d340a5563b17395158 (patch) | |
tree | ca35d119d770835b59d34cdec93805190d3155d1 /lstring.c | |
parent | a4b96ce9a3305ae3585c0bb143fa7342c140f20b (diff) | |
download | lua-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.c | 13 |
1 files changed, 7 insertions, 6 deletions
@@ -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 | ||
40 | unsigned int luaS_hash (const char *str, size_t l) { | 40 | unsigned 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 | */ |
121 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { | 121 | static 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 | ||