Author: tfaber
Date: Sat Aug 30 07:01:47 2014
New Revision: 63980
URL:
http://svn.reactos.org/svn/reactos?rev=63980&view=rev
Log:
[EVENTVWR]
- Use strsafe functions
- Don't cast malloc
- PSTR/PWSTR typedefs are for null-terminated strings, which output buffers aren't
- "dwData" implies the value is data, not the size of data. Also, cbData is
specified in bytes (hence "cb")
- Avoid pointless use of HEAP_ZERO_MEMORY
Modified:
trunk/reactos/base/applications/mscutils/eventvwr/eventvwr.c
Modified: trunk/reactos/base/applications/mscutils/eventvwr/eventvwr.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/applications/mscutils…
==============================================================================
--- trunk/reactos/base/applications/mscutils/eventvwr/eventvwr.c [iso-8859-1] (original)
+++ trunk/reactos/base/applications/mscutils/eventvwr/eventvwr.c [iso-8859-1] Sat Aug 30
07:01:47 2014
@@ -33,6 +33,7 @@
#include <winreg.h>
#include <commctrl.h>
#include <commdlg.h>
+#include <strsafe.h>
#include "resource.h"
@@ -202,7 +203,7 @@
GetEventMessageFileDLL(IN LPCWSTR lpLogName,
IN LPCWSTR SourceName,
IN LPCWSTR EntryName,
- OUT LPWSTR ExpandedName)
+ OUT PWCHAR ExpandedName)
{
DWORD dwSize;
BYTE szModuleName[MAX_PATH];
@@ -211,8 +212,8 @@
HKEY hSourceKey = NULL;
BOOL bReturn = FALSE;
- wcscpy(szKeyName, L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\");
- wcscat(szKeyName, lpLogName);
+ StringCbCopyW(szKeyName, sizeof(szKeyName),
L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\");
+ StringCbCatW(szKeyName, sizeof(szKeyName), lpLogName);
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
szKeyName,
@@ -261,7 +262,7 @@
GetEventCategory(IN LPCWSTR KeyName,
IN LPCWSTR SourceName,
IN EVENTLOGRECORD *pevlr,
- OUT LPWSTR CategoryName)
+ OUT PWCHAR CategoryName)
{
HANDLE hLibrary = NULL;
WCHAR szMessageDLL[MAX_PATH];
@@ -287,7 +288,7 @@
TrimNulls(lpMsgBuf);
/* Copy the category name */
- wcscpy(CategoryName, lpMsgBuf);
+ StringCchCopyW(CategoryName, MAX_PATH, lpMsgBuf);
}
else
{
@@ -315,7 +316,7 @@
GetEventMessage(IN LPCWSTR KeyName,
IN LPCWSTR SourceName,
IN EVENTLOGRECORD *pevlr,
- OUT LPWSTR EventText)
+ OUT PWCHAR EventText)
{
DWORD i;
HANDLE hLibrary = NULL;
@@ -394,7 +395,7 @@
TrimNulls((LPWSTR)lpMsgBuf);
/* Copy the event text */
- wcscpy(EventText ,lpMsgBuf);
+ StringCchCopyW(EventText, EVENT_MESSAGE_EVENTTEXT_BUFFER,
lpMsgBuf);
}
}
@@ -405,7 +406,7 @@
if (!bDone)
{
LoadStringW(hInst, IDS_EVENTSTRINGIDNOTFOUND, szStringIDNotFound,
MAX_LOADSTRING);
- swprintf(EventText, szStringIDNotFound, (pevlr->EventID & 0xFFFF),
SourceName);
+ StringCchPrintfW(EventText, EVENT_MESSAGE_EVENTTEXT_BUFFER,
szStringIDNotFound, (pevlr->EventID & 0xFFFF), SourceName);
}
free(szArguments);
@@ -415,7 +416,7 @@
}
LoadStringW(hInst, IDS_EVENTSTRINGIDNOTFOUND, szStringIDNotFound, MAX_LOADSTRING);
- swprintf(EventText, szStringIDNotFound, (pevlr->EventID & 0xFFFF),
SourceName);
+ StringCchPrintfW(EventText, EVENT_MESSAGE_EVENTTEXT_BUFFER, szStringIDNotFound,
(pevlr->EventID & 0xFFFF), SourceName);
return FALSE;
}
@@ -423,7 +424,7 @@
VOID
GetEventType(IN WORD dwEventType,
- OUT LPWSTR eventTypeText)
+ OUT PWCHAR eventTypeText)
{
switch (dwEventType)
{
@@ -453,7 +454,7 @@
BOOL
GetEventUserName(EVENTLOGRECORD *pelr,
- OUT LPWSTR pszUser)
+ OUT PWCHAR pszUser)
{
PSID lpSid;
WCHAR szName[1024];
@@ -476,7 +477,7 @@
&cbDomain,
&peUse))
{
- wcscpy(pszUser, szName);
+ StringCchCopyW(pszUser, MAX_PATH, szName);
return TRUE;
}
}
@@ -523,23 +524,24 @@
HANDLE hEventLog;
EVENTLOGRECORD *pevlr;
DWORD dwRead, dwNeeded, dwThisRecord, dwTotalRecords = 0, dwCurrentRecord = 0,
dwRecordsToRead = 0, dwFlags, dwMaxLength;
+ size_t cchRemaining;
LPWSTR lpSourceName;
LPWSTR lpComputerName;
LPSTR lpData;
BOOL bResult = TRUE; /* Read succeeded. */
- int i;
WCHAR szWindowTitle[MAX_PATH];
WCHAR szStatusText[MAX_PATH];
WCHAR szLocalDate[MAX_PATH];
WCHAR szLocalTime[MAX_PATH];
WCHAR szEventID[MAX_PATH];
- WCHAR szEventTypeText[MAX_PATH];
+ WCHAR szEventTypeText[MAX_LOADSTRING];
WCHAR szCategoryID[MAX_PATH];
WCHAR szUsername[MAX_PATH];
WCHAR szEventText[EVENT_MESSAGE_FILE_BUFFER];
WCHAR szCategory[MAX_PATH];
WCHAR szData[MAX_PATH];
+ PWCHAR lpTitleTemplateEnd;
SYSTEMTIME time;
LVITEMW lviEventItem;
@@ -649,8 +651,8 @@
GetEventType(pevlr->EventType, szEventTypeText);
GetEventCategory(lpLogName, lpSourceName, pevlr, szCategory);
- swprintf(szEventID, L"%u", (pevlr->EventID & 0xFFFF));
- swprintf(szCategoryID, L"%u", pevlr->EventCategory);
+ StringCbPrintfW(szEventID, sizeof(szEventID), L"%u",
(pevlr->EventID & 0xFFFF));
+ StringCbPrintfW(szCategoryID, sizeof(szCategoryID), L"%u",
pevlr->EventCategory);
lviEventItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
lviEventItem.iItem = 0;
@@ -713,16 +715,20 @@
// All events loaded
EndDialog(hwndDlg, 0);
-
- i = swprintf(szWindowTitle, szTitleTemplate, szTitle, lpLogName); /* i = number of
characters written */
+ StringCchPrintfExW(szWindowTitle,
+ sizeof(szWindowTitle) / sizeof(WCHAR),
+ &lpTitleTemplateEnd,
+ &cchRemaining,
+ 0,
+ szTitleTemplate, szTitle, lpLogName); /* i = number of characters
written */
/* lpComputerName can be NULL here if no records was read */
- dwMaxLength = sizeof(szWindowTitle) / sizeof(WCHAR) - i;
- if(!lpComputerName)
- GetComputerNameW(szWindowTitle+i, &dwMaxLength);
+ dwMaxLength = cchRemaining;
+ if (!lpComputerName)
+ GetComputerNameW(lpTitleTemplateEnd, &dwMaxLength);
else
- _snwprintf(szWindowTitle+i, dwMaxLength, L"%s", lpComputerName);
-
- swprintf(szStatusText, szStatusBarTemplate, lpLogName, dwTotalRecords);
+ StringCchCopyW(lpTitleTemplateEnd, dwMaxLength, lpComputerName);
+
+ StringCbPrintfW(szStatusText, sizeof(szStatusText), szStatusBarTemplate, lpLogName,
dwTotalRecords);
// Update the status bar
SendMessageW(hwndStatus, SB_SETTEXT, (WPARAM)0, (LPARAM)szStatusText);
@@ -863,22 +869,24 @@
VOID
-GetDisplayNameFile(LPCWSTR lpLogName, LPWSTR lpModuleName)
+GetDisplayNameFile(IN LPCWSTR lpLogName,
+ OUT PWCHAR lpModuleName)
{
HKEY hKey;
WCHAR *KeyPath;
WCHAR szModuleName[MAX_PATH];
- DWORD dwData;
-
-
- KeyPath = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
(wcslen(EVENTLOG_BASE_KEY) + wcslen(lpLogName) + 1) * sizeof(WCHAR));
+ DWORD cbData;
+ DWORD cbKeyPath;
+
+ cbKeyPath = (wcslen(EVENTLOG_BASE_KEY) + wcslen(lpLogName) + 1) * sizeof(WCHAR);
+ KeyPath = HeapAlloc(GetProcessHeap(), 0, cbKeyPath);
if (!KeyPath)
{
return;
}
- wcscpy(KeyPath, EVENTLOG_BASE_KEY);
- wcscat(KeyPath, lpLogName);
+ StringCbCopyW(KeyPath, cbKeyPath, EVENTLOG_BASE_KEY);
+ StringCbCatW(KeyPath, cbKeyPath, lpLogName);
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, KeyPath, 0, KEY_READ, &hKey) !=
ERROR_SUCCESS)
{
@@ -886,9 +894,8 @@
return;
}
- dwData = MAX_PATH;
-
- if (RegQueryValueExW(hKey, L"DisplayNameFile", NULL, NULL,
(LPBYTE)szModuleName, &dwData) == ERROR_SUCCESS)
+ cbData = sizeof(szModuleName);
+ if (RegQueryValueExW(hKey, L"DisplayNameFile", NULL, NULL,
(LPBYTE)szModuleName, &cbData) == ERROR_SUCCESS)
{
ExpandEnvironmentStringsW(szModuleName, lpModuleName, MAX_PATH);
}
@@ -899,21 +906,23 @@
DWORD
-GetDisplayNameID(LPCWSTR lpLogName)
+GetDisplayNameID(IN LPCWSTR lpLogName)
{
HKEY hKey;
WCHAR *KeyPath;
DWORD dwMessageID = 0;
- DWORD dwData;
-
- KeyPath = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
(wcslen(EVENTLOG_BASE_KEY) + wcslen(lpLogName) + 1) * sizeof(WCHAR));
+ DWORD cbData;
+ DWORD cbKeyPath;
+
+ cbKeyPath = (wcslen(EVENTLOG_BASE_KEY) + wcslen(lpLogName) + 1) * sizeof(WCHAR);
+ KeyPath = HeapAlloc(GetProcessHeap(), 0, cbKeyPath);
if (!KeyPath)
{
return 0;
}
- wcscpy(KeyPath, EVENTLOG_BASE_KEY);
- wcscat(KeyPath, lpLogName);
+ StringCbCopyW(KeyPath, cbKeyPath, EVENTLOG_BASE_KEY);
+ StringCbCatW(KeyPath, cbKeyPath, lpLogName);
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, KeyPath, 0, KEY_READ, &hKey) !=
ERROR_SUCCESS)
{
@@ -921,9 +930,8 @@
return 0;
}
- dwData = sizeof(dwMessageID);
-
- RegQueryValueExW(hKey, L"DisplayNameID", NULL, NULL,
(LPBYTE)&dwMessageID, &dwData);
+ cbData = sizeof(dwMessageID);
+ RegQueryValueExW(hKey, L"DisplayNameID", NULL, NULL,
(LPBYTE)&dwMessageID, &cbData);
RegCloseKey(hKey);
HeapFree(GetProcessHeap(), 0, KeyPath);
@@ -961,7 +969,7 @@
return;
}
- LogNames = (WCHAR**)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (dwNumLogs + 1) *
sizeof(WCHAR*));
+ LogNames = HeapAlloc(GetProcessHeap(), 0, (dwNumLogs + 1) * sizeof(WCHAR*));
if (!LogNames)
{