V531. It is odd that a sizeof() operator is multiplied by sizeof().
V531 It is odd that a sizeof() operator is multiplied by sizeof(). Borne pphtmldrawer.cpp 258
CPPString CPPHtmlDrawer::GetStringFromDll(....)
{
....
TCHAR szTemp[256];
DWORD dwLen = ::LoadString(hInstDll, dwID,
szTemp, (sizeof(szTemp) * sizeof(TCHAR)));
....
}
The LoadString function takes the buffer's size in characters as the last argument. In the Unicode version of the application, we will tell the function that the buffer's size is larger than it is actually. This may cause a buffer overflow. Note that if we fix the code in the following way, it will not become correct at all: sizeof(szTemp) / sizeof(TCHAR).
Similar errors can be found in some other places:
V531 It is odd that a sizeof() operator is multiplied by sizeof(). fire pphtmldrawer.cpp 258
CPPString CPPHtmlDrawer::GetStringFromDll(....)
{
....
TCHAR szTemp[256];
DWORD dwLen = ::LoadString(hInstDll, dwID, szTemp,
(sizeof(szTemp) * sizeof(TCHAR)));
....
}
The LoadString function takes the buffer's size in characters as the last argument. In the Unicode version of the application, we will tell the function that the buffer's size is larger than it is actually. This may cause a buffer overflow. Note that if we fix the code in the following way, it will not become correct at all: sizeof(szTemp) / sizeof(TCHAR).
V531 It is odd that a sizeof() operator is multiplied by sizeof(). eventvwr eventvwr.c 1112
VOID
DisplayEvent(HWND hDlg)
{
WCHAR szEventType[MAX_PATH];
WCHAR szTime[MAX_PATH];
WCHAR szDate[MAX_PATH];
....
ListView_GetItemText(hwndListView, iIndex, 0, szEventType,
sizeof(szEventType) * sizeof(WCHAR));
ListView_GetItemText(hwndListView, iIndex, 1, szDate,
sizeof(szDate) * sizeof(WCHAR));
ListView_GetItemText(hwndListView, iIndex, 2, szTime,
sizeof(szTime) * sizeof(WCHAR));
....
}
V531 It is odd that a sizeof() operator is multiplied by sizeof(). eventvwr.c 1117
VOID
DisplayEvent(HWND hDlg)
{
WCHAR szEventType[MAX_PATH];
WCHAR szTime[MAX_PATH];
WCHAR szDate[MAX_PATH];
WCHAR szUser[MAX_PATH];
WCHAR szComputer[MAX_PATH];
....
ListView_GetItemText(...., sizeof(szEventType)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szDate)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szTime)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szSource)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szCategory)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szEventID)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szUser)*sizeof(WCHAR));
ListView_GetItemText(...., sizeof(szComputer)*sizeof(WCHAR));
....
}
Similar errors can be found in some other places:
V531 It is odd that a sizeof() operator is multiplied by sizeof(). httprequestsender.cpp 286
BOOL CHttpRequestSender::InternalSend()
{
....
TCHAR szBuffer[1024]=_T("");
DWORD dwBuffSize = sizeof(szBuffer)*sizeof(TCHAR);
....
}
V531 It is odd that a sizeof() operator is multiplied by sizeof(). tstrtfileaio.cpp 61
void
tstFileAioTestReadWriteBasic(...., uint32_t cMaxReqsInFlight)
{
/* Allocate request array. */
RTFILEAIOREQ *paReqs;
paReqs = (...., cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
RTTESTI_CHECK_RETV(paReqs);
RT_BZERO(..., sizeof(cMaxReqsInFlight) * sizeof(RTFILEAIOREQ));
/* Allocate array holding pointer to data buffers. */
void **papvBuf = (...., cMaxReqsInFlight * sizeof(void *));
....
}
V531 It is odd that a sizeof() operator is multiplied by sizeof(). cmGlobalVisualStudioGenerator.cxx 558
bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
const std::string& regKeyBase,
std::string& nextAvailableSubKeyName)
{
....
if (ERROR_SUCCESS == result) {
wchar_t subkeyname[256]; // <=
DWORD cch_subkeyname = sizeof(subkeyname) * sizeof(subkeyname[0]); // <=
wchar_t keyclass[256];
DWORD cch_keyclass = sizeof(keyclass) * sizeof(keyclass[0]);
FILETIME lastWriteTime;
lastWriteTime.dwHighDateTime = 0;
lastWriteTime.dwLowDateTime = 0;
while (ERROR_SUCCESS ==
RegEnumKeyExW(hkey, index, subkeyname, &cch_subkeyname, 0, keyclass,
&cch_keyclass, &lastWriteTime)) {
....
}
....
}
Similar errors can be found in some other places:
This is exactly the case when a reply to a comment turned into a small blog post. The power of ...