]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
LruMap bug fix and other polishing touches
authorChristos Tsantilas <chtsanti@users.sourceforge.net>
Wed, 15 May 2013 16:43:04 +0000 (19:43 +0300)
committerChristos Tsantilas <chtsanti@users.sourceforge.net>
Wed, 15 May 2013 16:43:04 +0000 (19:43 +0300)
The  LruMap::findEntry may return destroyed entries to the caller.

src/base/LruMap.h

index f018b70b413273c98402499453db46b8f849723b..506b0e0bf2a10ab3ea6cb14b1f7a3e1da0bcda6e 100644 (file)
@@ -59,7 +59,7 @@ private:
     LruMap(LruMap const &);
     LruMap & operator = (LruMap const &);
 
-    bool expired(Entry &e);
+    bool expired(const Entry &e) const;
     void trim();
     void touch(const MapIterator &i);
     bool del(const MapIterator &i);
@@ -110,12 +110,14 @@ LruMap<EntryValue, EntryCost>::findEntry(const char *key, LruMap::MapIterator &i
     index.erase(i->second);
     i->second = index.begin();
 
-    Entry *e = *i->second;
-
-    if (e && expired(*e)) {
-        del(i);
-        e = NULL;
+    if (const Entry *e = *i->second) {
+        if (!expired(*e))
+            return;
+        // else fall through to cleanup
     }
+
+    del(i);
+    i = storage.end();
 }
 
 template <class EntryValue, size_t EntryCost>
@@ -124,9 +126,9 @@ LruMap<EntryValue, EntryCost>::get(const char *key)
 {
     MapIterator i;
     findEntry(key, i);
-    Entry *e = *i->second;
     if (i != storage.end()) {
         touch(i);
+        Entry *e = *i->second;
         return e->value;
     }
     return NULL;
@@ -150,7 +152,7 @@ LruMap<EntryValue, EntryCost>::add(const char *key, EntryValue *t)
 
 template <class EntryValue, size_t EntryCost>
 bool
-LruMap<EntryValue, EntryCost>::expired(LruMap::Entry &entry)
+LruMap<EntryValue, EntryCost>::expired(const LruMap::Entry &entry) const
 {
     if (ttl < 0)
         return false;
@@ -163,9 +165,10 @@ bool
 LruMap<EntryValue, EntryCost>::del(LruMap::MapIterator const &i)
 {
     if (i != storage.end()) {
-        delete *(i->second);
+        LruMap::Entry *e = *i->second;
         index.erase(i->second);
         storage.erase(i);
+        delete e;
         --entries_;
         return true;
     }