crypto: fix another over-run in bio
authorFedor Indutny <fedor.indutny@gmail.com>
Sat, 3 Aug 2013 09:09:02 +0000 (13:09 +0400)
committerFedor Indutny <fedor.indutny@gmail.com>
Sat, 3 Aug 2013 10:04:55 +0000 (14:04 +0400)
When doing `FreeEmpty`, `NodeBIO` skips pre-allocated `head_` buffer.
However this might lead to double-freeing buffers since in `~NodeBIO()`
we're starting deallocation from `head_` buffer.

src/node_crypto_bio.cc

index 6783184..1f1eb3b 100644 (file)
@@ -232,9 +232,12 @@ void NodeBIO::FreeEmpty() {
   if (cur == write_head_ || cur == read_head_)
     return;
 
+  Buffer* prev = child;
   while (cur != read_head_) {
-    // Skip embedded buffer
+    // Skip embedded buffer, and continue deallocating again starting from it
     if (cur == &head_) {
+      prev->next_ = cur;
+      prev = cur;
       cur = head_.next_;
       continue;
     }
@@ -242,11 +245,11 @@ void NodeBIO::FreeEmpty() {
     assert(cur->write_pos_ == cur->read_pos_);
 
     Buffer* next = cur->next_;
-    child->next_ = next;
     delete cur;
-
     cur = next;
   }
+  assert(prev == child || prev == &head_);
+  prev->next_ = cur;
 }