From e74b39deaf1aeb162ac2012b48be6bf137823f5a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 30 Jul 2014 22:31:48 +0900 Subject: [PATCH] Fix crash in OpenedFileCounter::ensureMaxOpenFileLimit() The crash happens if PieceStorage and/or DiskAdaptor are not initialized in one of active RequestGroups. --- src/OpenedFileCounter.cc | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/OpenedFileCounter.cc b/src/OpenedFileCounter.cc index c5bdeade..efdf8368 100644 --- a/src/OpenedFileCounter.cc +++ b/src/OpenedFileCounter.cc @@ -71,12 +71,32 @@ void OpenedFileCounter::ensureMaxOpenFileLimit(size_t numNewFiles) std::advance(mark, SimpleRandomizer::getInstance()-> getRandomNumber(requestGroups.size())); + + auto closeFun = [&left](const std::shared_ptr& group) + { + auto& ps = group->getPieceStorage(); + + if(!ps) { + return; + } + + auto diskAdaptor = ps->getDiskAdaptor(); + + if(!diskAdaptor) { + return; + } + + left -= diskAdaptor->tryCloseFile(left); + }; + for(auto i = mark; i != std::end(requestGroups) && left > 0; ++i) { - left -= (*i)->getPieceStorage()->getDiskAdaptor()->tryCloseFile(left); + closeFun(*i); } + for(auto i = std::begin(requestGroups); i != mark && left > 0; ++i) { - left -= (*i)->getPieceStorage()->getDiskAdaptor()->tryCloseFile(left); + closeFun(*i); } + assert(left == 0); numOpenFiles_ += numNewFiles - numClose; }