Skip to content

Commit

Permalink
use a map instead of a vector for holding plugin handles - ensures th…
Browse files Browse the repository at this point in the history
…at we don't attempt to access the wrong one if an earlier handle was removed. Thanks to ulion for finding the issue
  • Loading branch information
Jonathan Marshall committed Jan 4, 2013
1 parent 2c1be2c commit de8789a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 9 additions & 7 deletions xbmc/filesystem/PluginDirectory.cpp
Expand Up @@ -46,7 +46,8 @@ using namespace XFILE;
using namespace std;
using namespace ADDON;

vector<CPluginDirectory *> CPluginDirectory::globalHandles;
map<int, CPluginDirectory *> CPluginDirectory::globalHandles;
int CPluginDirectory::handleCounter = 0;
CCriticalSection CPluginDirectory::m_handleLock;

CPluginDirectory::CPluginDirectory()
Expand All @@ -64,23 +65,24 @@ CPluginDirectory::~CPluginDirectory(void)
int CPluginDirectory::getNewHandle(CPluginDirectory *cp)
{
CSingleLock lock(m_handleLock);
int handle = (int)globalHandles.size();
globalHandles.push_back(cp);
int handle = ++handleCounter;
globalHandles[handle] = cp;
return handle;
}

void CPluginDirectory::removeHandle(int handle)
{
CSingleLock lock(m_handleLock);
if (handle >= 0 && handle < (int)globalHandles.size())
globalHandles.erase(globalHandles.begin() + handle);
if (!globalHandles.erase(handle))
CLog::Log(LOGWARNING, "Attempt to erase invalid handle %i", handle);
}

CPluginDirectory *CPluginDirectory::dirFromHandle(int handle)
{
CSingleLock lock(m_handleLock);
if (handle >= 0 && handle < (int)globalHandles.size())
return globalHandles[handle];
map<int, CPluginDirectory *>::iterator i = globalHandles.find(handle);
if (i != globalHandles.end())
return i->second;
CLog::Log(LOGWARNING, "Attempt to use invalid handle %i", handle);
return NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions xbmc/filesystem/PluginDirectory.h
Expand Up @@ -25,7 +25,7 @@
#include "SortFileItem.h"

#include <string>
#include <vector>
#include <map>
#include "threads/CriticalSection.h"
#include "addons/IAddon.h"
#include "PlatformDefs.h"
Expand Down Expand Up @@ -68,11 +68,12 @@ class CPluginDirectory : public IDirectory
bool StartScript(const CStdString& strPath, bool retrievingDir);
bool WaitOnScriptResult(const CStdString &scriptPath, int scriptId, const CStdString &scriptName, bool retrievingDir);

static std::vector<CPluginDirectory*> globalHandles;
static std::map<int,CPluginDirectory*> globalHandles;
static int getNewHandle(CPluginDirectory *cp);
static void removeHandle(int handle);
static CPluginDirectory *dirFromHandle(int handle);
static CCriticalSection m_handleLock;
static int handleCounter;

CFileItemList* m_listItems;
CFileItem* m_fileResult;
Expand Down

0 comments on commit de8789a

Please sign in to comment.