]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: always incorporate hash seed at start of hash computation
authorIan Lance Taylor <iant@golang.org>
Fri, 4 Jan 2013 15:53:42 +0000 (07:53 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 4 Jan 2013 15:53:42 +0000 (07:53 -0800)
Otherwise we can get predictable collisions.

R=golang-dev, dave, patrick, rsc
CC=golang-dev
https://golang.org/cl/7051043

src/pkg/runtime/alg.c
src/pkg/runtime/iface.c
src/pkg/runtime/runtime.h

index c7424bc26242897e94ae3980356dc868188dc253..ad85b43aefc1d9a6395de57463978d0ad3f68f4c 100644 (file)
@@ -19,13 +19,13 @@ runtime·memhash(uintptr *h, uintptr s, void *a)
        uintptr hash;
 
        b = a;
-       hash = M0;
+       hash = M0 ^ *h;
        while(s > 0) {
                hash = (hash ^ *b) * M1;
                b++;
                s--;
        }
-       *h = (*h ^ hash) * M1;
+       *h = hash;
 }
 
 void
@@ -355,7 +355,7 @@ void
 runtime·interhash(uintptr *h, uintptr s, void *a)
 {
        USED(s);
-       *h = (*h ^ runtime·ifacehash(*(Iface*)a)) * M1;
+       *h = runtime·ifacehash(*(Iface*)a, *h ^ M0) * M1;
 }
 
 void
@@ -389,7 +389,7 @@ void
 runtime·nilinterhash(uintptr *h, uintptr s, void *a)
 {
        USED(s);
-       *h = (*h ^ runtime·efacehash(*(Eface*)a)) * M1;
+       *h = runtime·efacehash(*(Eface*)a, *h ^ M0) * M1;
 }
 
 void
index 3a7c45fd140f576fcf559c40b1e035476c41d7cc..370edffb817e47085b02788c617323b35e66e74e 100644 (file)
@@ -546,10 +546,10 @@ runtime·assertE2E2(InterfaceType* inter, Eface e, Eface ret, bool ok)
 }
 
 static uintptr
-ifacehash1(void *data, Type *t)
+ifacehash1(void *data, Type *t, uintptr h)
 {
        Alg *alg;
-       uintptr size, h;
+       uintptr size;
        Eface err;
 
        if(t == nil)
@@ -563,7 +563,6 @@ ifacehash1(void *data, Type *t)
                runtime·newErrorString(runtime·catstring(runtime·gostringnocopy((byte*)"hash of unhashable type "), *t->string), &err);
                runtime·panic(err);
        }
-       h = 0;
        if(size <= sizeof(data))
                alg->hash(&h, size, &data);
        else
@@ -572,17 +571,17 @@ ifacehash1(void *data, Type *t)
 }
 
 uintptr
-runtime·ifacehash(Iface a)
+runtime·ifacehash(Iface a, uintptr h)
 {
        if(a.tab == nil)
-               return 0;
-       return ifacehash1(a.data, a.tab->type);
+               return h;
+       return ifacehash1(a.data, a.tab->type, h);
 }
 
 uintptr
-runtime·efacehash(Eface a)
+runtime·efacehash(Eface a, uintptr h)
 {
-       return ifacehash1(a.data, a.type);
+       return ifacehash1(a.data, a.type, h);
 }
 
 static bool
index 0c941f819b18b03184fc3a65124255c5d1a1e97a..a228b06e322e826b0177ff245044fe76105d9466 100644 (file)
@@ -644,8 +644,8 @@ void        runtime·freemcache(MCache*);
 void   runtime·mallocinit(void);
 bool   runtime·ifaceeq_c(Iface, Iface);
 bool   runtime·efaceeq_c(Eface, Eface);
-uintptr        runtime·ifacehash(Iface);
-uintptr        runtime·efacehash(Eface);
+uintptr        runtime·ifacehash(Iface, uintptr);
+uintptr        runtime·efacehash(Eface, uintptr);
 void*  runtime·malloc(uintptr size);
 void   runtime·free(void *v);
 bool   runtime·addfinalizer(void*, void(*fn)(void*), uintptr);