Index: TortoiseOverlays/IconOverlay.cpp
===================================================================
--- TortoiseOverlays/IconOverlay.cpp	(revision 20902)
+++ TortoiseOverlays/IconOverlay.cpp	(working copy)
@@ -10,6 +10,21 @@
 //  that file. The Shell then adds the icon overlay to the system image list."
 STDMETHODIMP CShellExt::GetOverlayInfo(LPWSTR pwszIconFile, int cchMax, int *pIndex, DWORD *pdwFlags)
 {
+    if(pwszIconFile == 0)
+        return E_POINTER;
+    if(pIndex == 0)
+        return E_POINTER;
+    if(pdwFlags == 0)
+        return E_POINTER;
+    if(cchMax < 1)
+        return E_INVALIDARG;
+
+    // Set "out parameters" in case we return S_FALSE or any code called from here
+    // forgets to set them.
+    *pwszIconFile = 0;
+    *pIndex = 0;
+    *pdwFlags = 0;
+
     int nInstalledOverlays = GetInstalledOverlays();
 
     // only a limited number of overlay slots can be used (determined by testing,
@@ -23,7 +38,6 @@
 
     const int nOverlayLimit = 12;
 
-
     bool dropIgnored = DropHandler(_T("ShowIgnoredOverlay"));
     if (dropIgnored)
         nInstalledOverlays--;
@@ -83,16 +97,8 @@
     if ((m_State == FileStateLocked)&&(nInstalledOverlays > nOverlayLimit))
         return S_FALSE;     // don't show the 'locked' overlay
 
-    // Get folder icons from registry
-    // Default icons are stored in LOCAL MACHINE
-    // User selected icons are stored in CURRENT USER
-    TCHAR regVal[1024];
-    DWORD len = 1024;
-
-    wstring icon;
     wstring iconName;
 
-    HKEY hkeys [] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
     switch (m_State)
     {
         case FileStateNormal        : iconName = _T("NormalIcon"); break;
@@ -106,28 +112,41 @@
         case FileStateUnversioned   : iconName = _T("UnversionedIcon"); break;
     }
 
-    for (int i = 0; i < 2; ++i)
+    // Get folder icons from registry
+    // Default icons are stored in LOCAL MACHINE
+    // User selected icons are stored in CURRENT USER
+    TCHAR regVal[1024];
+
+    wstring icon;
+    HKEY hkeys [] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
+    for (int i = 0; i < _countof(hkeys); ++i)
     {
+        if (!icon.empty())
+            continue;
+
         HKEY hkey = 0;
-
         if (::RegOpenKeyEx (hkeys[i],
             _T("Software\\TortoiseOverlays"),
                     0,
                     KEY_QUERY_VALUE,
                     &hkey) != ERROR_SUCCESS)
+        {
             continue;
+        }
 
-        if (icon.empty() == true
-            && (::RegQueryValueEx (hkey,
+        // in-out parameter, needs to be reinitialized prior to the call
+        DWORD len = _countof(regVal);
+        if (::RegQueryValueEx (hkey,
                              iconName.c_str(),
                              NULL,
                              NULL,
                              (LPBYTE) regVal,
                              &len)) == ERROR_SUCCESS)
+        {
             icon.assign (regVal, len);
+        }
 
         ::RegCloseKey(hkey);
-
     }
 
     // now load the Tortoise handlers and call their GetOverlayInfo method
@@ -136,15 +155,18 @@
     LoadHandlers(pwszIconFile, cchMax, pIndex, pdwFlags);
 
     // Add name of appropriate icon
-    if (icon.empty() == false)
-        wcsncpy_s (pwszIconFile, cchMax, icon.c_str(), cchMax);
-    else
+    if (icon.empty())
         return S_FALSE;
 
+    if (icon.size() >= cchMax)
+        return E_INVALIDARG;
+
+    wcsncpy_s (pwszIconFile, cchMax, icon.c_str(), cchMax);
+
     *pIndex = 0;
     *pdwFlags = ISIOI_ICONFILE;
     return S_OK;
-};
+}
 
 STDMETHODIMP CShellExt::GetPriority(int *pPriority)
 {
Index: TortoiseOverlays/ShellExt.h
===================================================================
--- TortoiseOverlays/ShellExt.h	(revision 20902)
+++ TortoiseOverlays/ShellExt.h	(working copy)
@@ -25,9 +25,8 @@
     FileStateInvalid
 };
 
-class DLLPointers
+struct DLLPointers
 {
-public:
     DLLPointers() : hDll(NULL)
         , pDllGetClassObject(NULL)
         , pDllCanUnloadNow(NULL)
Index: TortoiseShell/IconOverlay.cpp
===================================================================
--- TortoiseShell/IconOverlay.cpp	(revision 20902)
+++ TortoiseShell/IconOverlay.cpp	(working copy)
@@ -29,10 +29,22 @@
 //  the name of the file containing the overlay image, and its index within
 //  that file. The Shell then adds the icon overlay to the system image list."
 
-STDMETHODIMP CShellExt::GetOverlayInfo(LPWSTR /*pwszIconFile*/, int /*cchMax*/, int * /*pIndex*/, DWORD * /*pdwFlags*/)
+STDMETHODIMP CShellExt::GetOverlayInfo(LPWSTR pwszIconFile, int cchMax, int* pIndex, DWORD* pdwFlags)
 {
-    PreserveChdir preserveChdir;
+    if(pwszIconFile == 0)
+        return E_POINTER;
+    if(pIndex == 0)
+        return E_POINTER;
+    if(pdwFlags == 0)
+        return E_POINTER;
+    if(cchMax < 1)
+        return E_INVALIDARG;
 
+    // Set "out parameters" since we return S_OK later.
+    *pwszIconFile = 0;
+    *pIndex = 0;
+    *pdwFlags = 0;
+
     // Now here's where we can find out if due to lack of enough overlay
     // slots some of our overlays won't be shown.
     // To do that we have to mark every overlay handler that's successfully
@@ -53,7 +65,7 @@
     // we don't have to set the icon file and/or the index here:
     // the icons are handled by the TortoiseOverlays dll.
     return S_OK;
-};
+}
 
 STDMETHODIMP CShellExt::GetPriority(int *pPriority)
 {
