Skip to content
This repository
Browse code

use a map instead of a vector for holding plugin handles - ensures th…

…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...
commit de8789a5ec3cb6a7b82d7e304d8755a59dd0c20d 1 parent 2c1be2c
authored January 04, 2013
16  xbmc/filesystem/PluginDirectory.cpp
@@ -46,7 +46,8 @@ using namespace XFILE;
46 46
 using namespace std;
47 47
 using namespace ADDON;
48 48
 
49  
-vector<CPluginDirectory *> CPluginDirectory::globalHandles;
  49
+map<int, CPluginDirectory *> CPluginDirectory::globalHandles;
  50
+int CPluginDirectory::handleCounter = 0;
50 51
 CCriticalSection CPluginDirectory::m_handleLock;
51 52
 
52 53
 CPluginDirectory::CPluginDirectory()
@@ -64,23 +65,24 @@ CPluginDirectory::~CPluginDirectory(void)
64 65
 int CPluginDirectory::getNewHandle(CPluginDirectory *cp)
65 66
 {
66 67
   CSingleLock lock(m_handleLock);
67  
-  int handle = (int)globalHandles.size();
68  
-  globalHandles.push_back(cp);
  68
+  int handle = ++handleCounter;
  69
+  globalHandles[handle] = cp;
69 70
   return handle;
70 71
 }
71 72
 
72 73
 void CPluginDirectory::removeHandle(int handle)
73 74
 {
74 75
   CSingleLock lock(m_handleLock);
75  
-  if (handle >= 0 && handle < (int)globalHandles.size())
76  
-    globalHandles.erase(globalHandles.begin() + handle);
  76
+  if (!globalHandles.erase(handle))
  77
+    CLog::Log(LOGWARNING, "Attempt to erase invalid handle %i", handle);
77 78
 }
78 79
 
79 80
 CPluginDirectory *CPluginDirectory::dirFromHandle(int handle)
80 81
 {
81 82
   CSingleLock lock(m_handleLock);
82  
-  if (handle >= 0 && handle < (int)globalHandles.size())
83  
-    return globalHandles[handle];
  83
+  map<int, CPluginDirectory *>::iterator i = globalHandles.find(handle);
  84
+  if (i != globalHandles.end())
  85
+    return i->second;
84 86
   CLog::Log(LOGWARNING, "Attempt to use invalid handle %i", handle);
85 87
   return NULL;
86 88
 }
5  xbmc/filesystem/PluginDirectory.h
@@ -25,7 +25,7 @@
25 25
 #include "SortFileItem.h"
26 26
 
27 27
 #include <string>
28  
-#include <vector>
  28
+#include <map>
29 29
 #include "threads/CriticalSection.h"
30 30
 #include "addons/IAddon.h"
31 31
 #include "PlatformDefs.h"
@@ -68,11 +68,12 @@ class CPluginDirectory : public IDirectory
68 68
   bool StartScript(const CStdString& strPath, bool retrievingDir);
69 69
   bool WaitOnScriptResult(const CStdString &scriptPath, int scriptId, const CStdString &scriptName, bool retrievingDir);
70 70
 
71  
-  static std::vector<CPluginDirectory*> globalHandles;
  71
+  static std::map<int,CPluginDirectory*> globalHandles;
72 72
   static int getNewHandle(CPluginDirectory *cp);
73 73
   static void removeHandle(int handle);
74 74
   static CPluginDirectory *dirFromHandle(int handle);
75 75
   static CCriticalSection m_handleLock;
  76
+  static int handleCounter;
76 77
 
77 78
   CFileItemList* m_listItems;
78 79
   CFileItem*     m_fileResult;

0 notes on commit de8789a

Please sign in to comment.
Something went wrong with that request. Please try again.