fixed buffer overflow
[reactos.git] / reactos / subsys / system / regedit / listview.c
index 2aea225..c88e26d 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 
 #include "main.h"
+#include "regproc.h"
 
 #define CX_ICON    16
 #define CY_ICON    16
@@ -66,7 +67,7 @@ LPCTSTR GetValueName(HWND hwndLV, int iStartAt)
     if (!g_valueName) return NULL;
     *g_valueName = 0;
     maxLen = HeapSize(GetProcessHeap(), 0, g_valueName);
-    if (maxLen == (SIZE_T) - 1) return NULL;
+    if (maxLen == -1) return NULL;
 
     item = ListView_GetNextItem(hwndLV, iStartAt, LVNI_SELECTED);
     if (item == -1) return NULL;
@@ -100,7 +101,7 @@ BOOL IsDefaultValue(HWND hwndLV, int i)
 {
   PLINE_INFO lineinfo;
   LVITEM Item;
-  
+
   Item.mask = LVIF_PARAM;
   Item.iItem = i;
   if(ListView_GetItem(hwndLV, &Item))
@@ -176,7 +177,7 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType, void* ValB
             break;
         case REG_MULTI_SZ:
             {
-              LPTSTR src, str, cursrc;
+              LPTSTR src, str;
               if(dwCount >= 2)
               {
                  src = (LPTSTR)ValBuf;
@@ -222,7 +223,7 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType, void* ValB
                 LPTSTR strBinary;
                 if(dwCount > 0)
                 {
-                   strBinary = HeapAlloc(GetProcessHeap(), 0, (dwCount * sizeof(TCHAR) * 3) + 1);
+                    strBinary = HeapAlloc(GetProcessHeap(), 0, (dwCount * sizeof(TCHAR) * 3) + sizeof(TCHAR));
                     for (i = 0; i < dwCount; i++)
                     {
                         wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
@@ -234,8 +235,8 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType, void* ValB
                 else
                 {
                     TCHAR szText[128];
-                   LoadString(hInst, IDS_BINARY_EMPTY, szText, sizeof(szText)/sizeof(TCHAR));
-                   ListView_SetItemText(hwndLV, index, 2, szText);
+                    LoadString(hInst, IDS_BINARY_EMPTY, szText, sizeof(szText)/sizeof(TCHAR));
+                    ListView_SetItemText(hwndLV, index, 2, szText);
                 }
             }
             break;
@@ -276,10 +277,10 @@ static BOOL InitListViewImageLists(HWND hwndLV)
 
     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_BIN));
     Image_Bin = ImageList_AddIcon(himl, hico);
-    
+
     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_STRING));
     Image_String = ImageList_AddIcon(himl, hico);
-    
+
 
     /* Fail if not all of the images were added.  */
     if (ImageList_GetImageCount(himl) < NUM_ICONS)
@@ -309,6 +310,9 @@ static void OnGetDispInfo(NMLVDISPINFO* plvdi)
         break;
     case 1:
         switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
+        case REG_NONE:
+            plvdi->item.pszText = _T("REG_NONE");
+            break;
         case REG_SZ:
             plvdi->item.pszText = _T("REG_SZ");
             break;
@@ -318,18 +322,18 @@ static void OnGetDispInfo(NMLVDISPINFO* plvdi)
         case REG_BINARY:
             plvdi->item.pszText = _T("REG_BINARY");
             break;
-        case REG_DWORD:
+        case REG_DWORD: /* REG_DWORD_LITTLE_ENDIAN */
             plvdi->item.pszText = _T("REG_DWORD");
             break;
         case REG_DWORD_BIG_ENDIAN:
             plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
             break;
-        case REG_MULTI_SZ:
-            plvdi->item.pszText = _T("REG_MULTI_SZ");
-            break;
         case REG_LINK:
             plvdi->item.pszText = _T("REG_LINK");
             break;
+        case REG_MULTI_SZ:
+            plvdi->item.pszText = _T("REG_MULTI_SZ");
+            break;
         case REG_RESOURCE_LIST:
             plvdi->item.pszText = _T("REG_RESOURCE_LIST");
             break;
@@ -339,8 +343,8 @@ static void OnGetDispInfo(NMLVDISPINFO* plvdi)
         case REG_RESOURCE_REQUIREMENTS_LIST:
             plvdi->item.pszText = _T("REG_RESOURCE_REQUIREMENTS_LIST");
             break;
-        case REG_NONE:
-            plvdi->item.pszText = _T("REG_NONE");
+        case REG_QWORD: /* REG_QWORD_LITTLE_ENDIAN */
+            plvdi->item.pszText = _T("REG_QWORD");
             break;
         default: {
             TCHAR buf2[200];
@@ -362,10 +366,10 @@ static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSor
     LINE_INFO*l, *r;
     l = (LINE_INFO*)lParam1;
     r = (LINE_INFO*)lParam2;
-        
-    if (g_columnToSort == ~0UL) 
+
+    if (g_columnToSort == ~0UL)
         g_columnToSort = 0;
-    
+
     if (g_columnToSort == 1 && l->dwValType != r->dwValType)
         return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
     if (g_columnToSort == 2) {
@@ -374,17 +378,6 @@ static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSor
     return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
 }
 
-static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
-{
-    switch (LOWORD(wParam)) {
-        /*    case ID_FILE_OPEN: */
-        /*        break; */
-    default:
-        return FALSE;
-    }
-    return TRUE;
-}
-
 BOOL ListWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
 {
     NMLVDISPINFO* Info;
@@ -394,13 +387,13 @@ BOOL ListWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
             OnGetDispInfo((NMLVDISPINFO*)lParam);
             return TRUE;
         case LVN_COLUMNCLICK:
-            if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
+            if (g_columnToSort == (DWORD)((LPNMLISTVIEW)lParam)->iSubItem)
                 g_invertSort = !g_invertSort;
             else {
                 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
                 g_invertSort = FALSE;
             }
-                    
+
             ListView_SortItems(hWnd, CompareFunc, (WPARAM)hWnd);
             return TRUE;
         case NM_DBLCLK:
@@ -445,20 +438,27 @@ BOOL ListWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
                 }
                 else
                 {
-                  LONG ret;
                  //if((ret = RenameValue(lineinfo->name, Info->item.pszText)) != ERROR_SUCCESS)
                   {
                    TCHAR msg[128], caption[128];
-                   
+
                    LoadString(hInst, IDS_ERR_RENVAL_CAPTION, caption, sizeof(caption)/sizeof(TCHAR));
                    if(_tcslen(Info->item.pszText) == 0)
                    {
                      LoadString(hInst, IDS_ERR_RENVAL_TOEMPTY, msg, sizeof(msg)/sizeof(TCHAR));
+                     MessageBox(0, msg, NULL, 0);
+                     *Result = TRUE;
                    }
                    else
-                    _stprintf(msg, _T("rename from %s to %s"), lineinfo->name, Info->item.pszText);
-                    MessageBox(0, msg, NULL, 0);
-                   *Result = TRUE;
+                       {
+                         HKEY hKeyRoot;
+                         LPCTSTR keyPath;
+                         LONG lResult;
+                         keyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
+                         lResult = RegRenameValue(hKeyRoot, keyPath, Info->item.pszText, lineinfo->name);
+                     *Result = TRUE;
+                     return (lResult == ERROR_SUCCESS);
+                       }
                  }
                 }
               }
@@ -483,7 +483,7 @@ HWND CreateListView(HWND hwndParent, int id)
                             0, 0, rcClient.right, rcClient.bottom,
                             hwndParent, (HMENU)id, hInst, NULL);
     if (!hwndLV) return NULL;
-    
+
     /* Initialize the image list, and add items to the control.  */
     if (!CreateListColumns(hwndLV)) goto fail;
     if (!InitListViewImageLists(hwndLV)) goto fail;
@@ -507,9 +507,9 @@ BOOL RefreshListView(HWND hwndLV, HKEY hKey, LPCTSTR keyPath)
     BOOL AddedDefault = FALSE;
 
     if (!hwndLV) return FALSE;
-    
+
     ListView_EditLabel(hwndLV, -1);
-    
+
     SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
     count = ListView_GetItemCount(hwndLV);
     for (i = 0; i < count; i++) {
@@ -521,14 +521,14 @@ BOOL RefreshListView(HWND hwndLV, HKEY hKey, LPCTSTR keyPath)
     }
     g_columnToSort = ~0UL;
     ListView_DeleteAllItems(hwndLV);
-    
+
     if(!hKey) return FALSE;
 
     errCode = RegOpenKeyEx(hKey, keyPath, 0, KEY_READ, &hNewKey);
     if (errCode != ERROR_SUCCESS) return FALSE;
 
     /* get size information and resize the buffers if necessary */
-    errCode = RegQueryInfoKey(hNewKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL, 
+    errCode = RegQueryInfoKey(hNewKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
                               &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
 
     #define BUF_HEAD_SPACE 2 /* FIXME: check why this is required with ROS ??? */