Loop-detection in assoc_find..

Trond Norbye Trond.Norbye at Sun.COM
Fri Apr 18 20:25:38 UTC 2008


Hi,

We have had some discussions lately about a loop in assoc_find, and  
since we currently haven't been able to reproduce this problem in our  
test environment I think that we should add loop detection into the  
assoc_find routine.

The following is a patch (for the binary branch) that implements this.

Trond

Index: assoc.c
===================================================================
--- assoc.c     (revision 778)
+++ assoc.c     (working copy)
@@ -489,7 +489,10 @@
      uint32_t hv = hash(key, nkey, 0);
      item *it;
      unsigned int oldbucket;
-
+#ifndef NDEBUG
+    item *lookahead;
+#endif
+
      if (expanding &&
          (oldbucket = (hv & hashmask(hashpower - 1))) >= expand_bucket)
      {
@@ -498,7 +501,22 @@
          it = primary_hashtable[hv & hashmask(hashpower)];
      }

+#ifndef NDEBUG
+    lookahead = it;
+#endif
+
      while (it) {
+#ifndef NDEBUG
+        if (lookahead && lookahead->h_next) {
+            lookahead = lookahead->h_next->h_next;
+            if (lookahead == it) {
+                fprintf(stderr,
+                        "ERROR: Loop detected inside the assoc table! 
%s\n",
+                        expanding ? " during expansion of the  
hashtable" : "");
+                assert(false);
+            }
+        }
+#endif
          if ((nkey == it->nkey) &&
              (memcmp(key, ITEM_key(it), nkey) == 0)) {
              return it;



More information about the memcached mailing list