Skip to content

Commit

Permalink
Opt, 消除代码中大多数警告
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed May 9, 2024
1 parent f47ecd7 commit 2e1d5b7
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 103 deletions.
6 changes: 4 additions & 2 deletions src/Shared/km.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <Windows.h>
#include <winnt.h>

//#pragma comment(lib,"ntdll.lib")
#pragma warning(push)
// 忽略非标准的 0 数组警告。
#pragma warning(disable:4200)


#define FILE_SUPERSEDE 0x00000000
Expand Down Expand Up @@ -5202,7 +5204,7 @@ typedef struct _KUSER_SHARED_DATA
#define KI_USER_SHARED_DATA 0x7ffe0000
const auto SharedUserData = reinterpret_cast<const KUSER_SHARED_DATA*>(KI_USER_SHARED_DATA);


#pragma warning(pop)
#if defined __cplusplus && !defined _Disallow_YY_KM_Namespace
} //namespace YY
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Thunks/Iphlpapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ namespace YY
return ERROR_INVALID_PARAMETER;

MIB_IFROW _IfRow;
_IfRow.dwIndex = _pRow->InterfaceLuid.Value ? _pRow->InterfaceLuid.Info.NetLuidIndex : _pRow->InterfaceIndex;
_IfRow.dwIndex = _pRow->InterfaceLuid.Value ? static_cast<ULONG>(_pRow->InterfaceLuid.Info.NetLuidIndex) : _pRow->InterfaceIndex;

auto _lStatus = GetIfEntry(&_IfRow);
if (NO_ERROR != _lStatus)
Expand Down
41 changes: 38 additions & 3 deletions src/Thunks/YY_Thunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,37 @@ namespace YY
{
namespace internal
{
inline UINT8 __fastcall BitsCount(ULONG32 _fBitMask)
{
#if defined(_M_IX86) || defined(_M_AMD64)
return static_cast<UINT8>(__popcnt(_fBitMask));
#else
_fBitMask = (_fBitMask & 0x55555555) + ((_fBitMask >> 1) & 0x55555555);
_fBitMask = (_fBitMask & 0x33333333) + ((_fBitMask >> 2) & 0x33333333);
_fBitMask = (_fBitMask & 0x0f0f0f0f) + ((_fBitMask >> 4) & 0x0f0f0f0f);
_fBitMask = (_fBitMask & 0x00ff00ff) + ((_fBitMask >> 8) & 0x00ff00ff);
_fBitMask = (_fBitMask & 0x0000ffff) + ((_fBitMask >> 16) & 0x0000ffff);
return static_cast<UINT8>(_fBitMask);
#endif
}

inline UINT8 __fastcall BitsCount(ULONG64 _fBitMask)
{
#if defined(_M_IX86)
return static_cast<UINT8>(__popcnt(static_cast<ULONG32>(_fBitMask)) + __popcnt(static_cast<ULONG32>(_fBitMask >> 32)));
#elif defined(_M_AMD64)
return static_cast<UINT8>(__popcnt64(_fBitMask));
#else
_fBitMask = (_fBitMask & 0x55555555'55555555) + ((_fBitMask >> 1) & 0x55555555'55555555);
_fBitMask = (_fBitMask & 0x33333333'33333333) + ((_fBitMask >> 2) & 0x33333333'33333333);
_fBitMask = (_fBitMask & 0x0f0f0f0f'0f0f0f0f) + ((_fBitMask >> 4) & 0x0f0f0f0f'0f0f0f0f);
_fBitMask = (_fBitMask & 0x00ff00ff'00ff00ff) + ((_fBitMask >> 8) & 0x00ff00ff'00ff00ff);
_fBitMask = (_fBitMask & 0x0000ffff'0000ffff) + ((_fBitMask >> 16) & 0x0000ffff'0000ffff);
_fBitMask = (_fBitMask & 0x00000000'ffffffff) + ((_fBitMask >> 32) & 0x00000000'ffffffff);
return static_cast<UINT8>(_fBitMask);
#endif
}

__forceinline constexpr DWORD __fastcall MakeVersion(_In_ DWORD _uMajorVersion, _In_ DWORD _uMinorVersion)
{
return (_uMajorVersion << 16) | _uMinorVersion;
Expand Down Expand Up @@ -234,13 +265,17 @@ namespace YY
const UINT CodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;

auto cchDst = MultiByteToWideChar(CodePage, MB_ERR_INVALID_CHARS, Src, -1, pDst->Buffer, pDst->MaximumLength / sizeof(wchar_t));
if (cchDst == 0)
if (cchDst <= 0)
{
return GetLastError();
}
cchDst *= sizeof(wchar_t);
if (cchDst > MAXUINT16)
{
return ERROR_BAD_PATHNAME;
}

pDst->Length = cchDst * sizeof(wchar_t);

pDst->Length = static_cast<USHORT>(cchDst);
return ERROR_SUCCESS;
}

Expand Down
50 changes: 41 additions & 9 deletions src/Thunks/api-ms-win-core-file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ namespace YY
return FALSE;
}

auto dwNewBufferSize = sizeof(FILE_RENAME_INFO) + NtName.Length;
DWORD dwNewBufferSize = sizeof(FILE_RENAME_INFO) + NtName.Length;

auto NewRenameInfo = (FILE_RENAME_INFO*)HeapAlloc(ProcessHeap, 0, dwNewBufferSize);
if (!NewRenameInfo)
Expand Down Expand Up @@ -533,8 +533,14 @@ namespace YY
goto __Exit;
}

const auto _uNewLength = pObjectName->Name.Length - pFileNameInfo->FileNameLength + sizeof(wchar_t);
if (_uNewLength > MAXUINT16)
{
lStatus = ERROR_BAD_PATHNAME;
goto __Exit;
}
VolumeNtName.Buffer = pObjectName->Name.Buffer;
VolumeNtName.Length = VolumeNtName.MaximumLength = pObjectName->Name.Length - pFileNameInfo->FileNameLength + sizeof(wchar_t);
VolumeNtName.Length = VolumeNtName.MaximumLength = static_cast<USHORT>(_uNewLength);


if (VOLUME_NAME_NT & dwFlags)
Expand Down Expand Up @@ -568,8 +574,14 @@ namespace YY
}
}

const auto _uNewLength = (wcslen(szVolumeRoot) - 1) * sizeof(szVolumeRoot[0]);
if (_uNewLength > MAXUINT16)
{
lStatus = ERROR_BAD_PATHNAME;
goto __Exit;
}
TargetVolumeName.Buffer = szVolumeRoot;
TargetVolumeName.Length = TargetVolumeName.MaximumLength = (wcslen(szVolumeRoot) - 1) * sizeof(szVolumeRoot[0]);
TargetVolumeName.Length = TargetVolumeName.MaximumLength = static_cast<USHORT>(_uNewLength);
}

//将路径进行规范化
Expand Down Expand Up @@ -597,7 +609,14 @@ namespace YY
}
}

cbszVolumeRoot = (wcslen(szVolumeRoot) - 1) * sizeof(szVolumeRoot[0]);
const auto _cbData = (wcslen(szVolumeRoot) - 1) * sizeof(szVolumeRoot[0]);
if (_cbData > MAXUINT16)
{
lStatus = ERROR_BAD_PATHNAME;
goto __Exit;
}

cbszVolumeRoot = static_cast<DWORD>(_cbData);
}


Expand All @@ -611,7 +630,7 @@ namespace YY
goto __Exit;
}

auto cchLongPathNameBufferSize = cbLongPathNameBufferSize / sizeof(szLongPathNameBuffer[0]);
DWORD cchLongPathNameBufferSize = cbLongPathNameBufferSize / sizeof(szLongPathNameBuffer[0]);

memcpy(szLongPathNameBuffer, szVolumeRoot, cbszVolumeRoot);
memcpy((char*)szLongPathNameBuffer + cbszVolumeRoot, pFileNameInfo->FileName, pFileNameInfo->FileNameLength);
Expand Down Expand Up @@ -643,18 +662,31 @@ namespace YY
}
else
{
const auto _uNewLength = result * sizeof(wchar_t) - cbszVolumeRoot;
if (_uNewLength > MAXUINT16)
{
lStatus = ERROR_BAD_PATHNAME;
goto __Exit;
}

//转换成功
TargetFileName.Buffer = (wchar_t*)((char*)szLongPathNameBuffer + cbszVolumeRoot);
TargetFileName.Length = TargetFileName.MaximumLength = result * sizeof(wchar_t) - cbszVolumeRoot;
TargetFileName.Length = TargetFileName.MaximumLength = static_cast<USHORT>(_uNewLength);
break;
}
}
}
else
{
if (pFileNameInfo->FileNameLength > MAXUINT16)
{
lStatus = ERROR_BAD_PATHNAME;
goto __Exit;
}

//直接返回原始路径
TargetFileName.Buffer = pFileNameInfo->FileName;
TargetFileName.Length = TargetFileName.MaximumLength = pFileNameInfo->FileNameLength;
TargetFileName.Length = TargetFileName.MaximumLength = static_cast<USHORT>(pFileNameInfo->FileNameLength);
}


Expand Down Expand Up @@ -769,11 +801,11 @@ namespace YY

auto cchReturnANSI = WideCharToMultiByte(CodePage, WC_NO_BEST_FIT_CHARS, szFilePathUnicode, cchReturn, nullptr, 0, nullptr, nullptr);

if (0 == cchReturnANSI)
if (cchReturnANSI <= 0)
{
goto __Error;
}
else if (cchReturnANSI >= cchFilePath)
else if (static_cast<DWORD>(cchReturnANSI) >= cchFilePath)
{
//长度不足
++cchReturnANSI;
Expand Down
20 changes: 13 additions & 7 deletions src/Thunks/api-ms-win-core-libraryloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ namespace YY

if (dwLoadLibrarySearchFlags & LOAD_LIBRARY_SEARCH_APPLICATION_DIR)
{
const auto nBufferMax = _countof(szFilePathBuffer) - nSize;
const DWORD nBufferMax = _countof(szFilePathBuffer) - nSize;

auto nBuffer = GetModuleFileNameW(NULL, szFilePathBuffer + nSize, nBufferMax);

Expand Down Expand Up @@ -544,7 +544,7 @@ namespace YY

if (dwLoadLibrarySearchFlags & LOAD_LIBRARY_SEARCH_SYSTEM32)
{
const auto nBufferMax = _countof(szFilePathBuffer) - nSize;
const DWORD nBufferMax = _countof(szFilePathBuffer) - nSize;

auto nBuffer = GetSystemDirectoryW(szFilePathBuffer + nSize, nBufferMax);

Expand All @@ -563,8 +563,14 @@ namespace YY
ModuleFileName.Buffer = (PWSTR)lpLibFileName;

for (; *lpLibFileName; ++lpLibFileName);
const auto _uNewLength = (lpLibFileName - ModuleFileName.Buffer) * sizeof(lpLibFileName[0]);
if (_uNewLength + sizeof(lpLibFileName[0]) > MAXUINT16)
{
SetLastError(ERROR_INVALID_PARAMETER);
return nullptr;
}

ModuleFileName.Length = (lpLibFileName - ModuleFileName.Buffer) * sizeof(lpLibFileName[0]);
ModuleFileName.Length = static_cast<USHORT>(_uNewLength);
ModuleFileName.MaximumLength = ModuleFileName.Length + sizeof(lpLibFileName[0]);

HMODULE hModule = NULL;
Expand Down Expand Up @@ -772,7 +778,7 @@ namespace YY

if (_cchSource == -1)
{
_cchSource = wcslen(_pStringSource);
_cchSource = (int)wcslen(_pStringSource);
}

if (_cchSource == 0)
Expand All @@ -782,7 +788,7 @@ namespace YY

if (_cchValue == -1)
{
_cchValue = wcslen(_pStringValue);
_cchValue = (int)wcslen(_pStringValue);
}

if (_cchValue == 0 || _cchValue > _cchSource)
Expand All @@ -798,7 +804,7 @@ namespace YY
{
if (CompareStringOrdinal(_pStart, _cchValue, _pStringValue, _cchValue, _bIgnoreCase) == CSTR_EQUAL)
{
return _pStart - _pStringSource;
return static_cast<int>(_pStart - _pStringSource);
}
}
return -1;
Expand All @@ -808,7 +814,7 @@ namespace YY
{
if (CompareStringOrdinal(_pStart, _cchValue, _pStringValue, _cchValue, _bIgnoreCase) == CSTR_EQUAL)
{
return _pStart - _pStringSource;
return static_cast<int>(_pStart - _pStringSource);
}
}
return -1;
Expand Down
18 changes: 9 additions & 9 deletions src/Thunks/api-ms-win-core-localization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ namespace YY

if (dwFlags & MUI_MERGE_SYSTEM_FALLBACK)
{
for (; LangID = internal::DownlevelGetFallbackLocaleLCID(LangID);)
for (; LangID = (LANGID)internal::DownlevelGetFallbackLocaleLCID(LangID);)
{
internal::AddLangIDToBuffer(LanguageIds, _countof(LanguageIds), ulNumLanguages, LangID);
}
Expand All @@ -2151,7 +2151,7 @@ namespace YY
internal::AddLangIDToBuffer(LanguageIds, _countof(LanguageIds), ulNumLanguages, LangID);


for (; LangID = internal::DownlevelGetFallbackLocaleLCID(LangID);)
for (; LangID = (LANGID)internal::DownlevelGetFallbackLocaleLCID(LangID);)
{
internal::AddLangIDToBuffer(LanguageIds, _countof(LanguageIds), ulNumLanguages, LangID);
}
Expand All @@ -2163,7 +2163,7 @@ namespace YY
internal::AddLangIDToBuffer(LanguageIds, _countof(LanguageIds), ulNumLanguages, LangID);


for (; LangID = internal::DownlevelGetFallbackLocaleLCID(LangID);)
for (; LangID = (LANGID)internal::DownlevelGetFallbackLocaleLCID(LangID);)
{
internal::AddLangIDToBuffer(LanguageIds, _countof(LanguageIds), ulNumLanguages, LangID);
}
Expand Down Expand Up @@ -2238,7 +2238,7 @@ namespace YY

*pTmp++ = L'\0';

cchLanguagesBufferNeed = pTmp - pBuffer;
cchLanguagesBufferNeed = static_cast<ULONG>(pTmp - pBuffer);

*pcchLanguagesBuffer = cchLanguagesBufferNeed;

Expand Down Expand Up @@ -2267,7 +2267,7 @@ namespace YY

if (result <= LOCALE_NAME_MAX_LENGTH)
{
cchLanguagesBufferNeed += result;
cchLanguagesBufferNeed += static_cast<ULONG>(result);
}
}
}
Expand Down Expand Up @@ -2306,12 +2306,12 @@ namespace YY
void
)
{
if (auto const pGetThreadUILanguage = try_get_GetThreadUILanguage())
if (auto const _pfnGetThreadUILanguage = try_get_GetThreadUILanguage())
{
return pGetThreadUILanguage();
return _pfnGetThreadUILanguage();
}

return GetThreadLocale();
return (LANGID)GetThreadLocale();
}
#endif

Expand Down Expand Up @@ -2678,7 +2678,7 @@ namespace YY
}

if (_cchStr < 0)
_cchStr = wcslen(_szString);
_cchStr = (int)wcslen(_szString);


for (; _cchStr;--_cchStr, ++_szString)
Expand Down
4 changes: 2 additions & 2 deletions src/Thunks/api-ms-win-core-path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ namespace YY
//处于长命名模式中,如果规范化后的路径有效部分小于 MAX_PATH,那么删除长命名前缀
if (ulReservedSize)
{
if (pTempOut - pszPathOut <= ulReservedSize + MAX_PATH)
if (size_t(pTempOut - pszPathOut) <= ulReservedSize + MAX_PATH)
{
if (bUNC)
{
Expand Down Expand Up @@ -1759,4 +1759,4 @@ namespace YY
#endif

}
}
}
Loading

0 comments on commit 2e1d5b7

Please sign in to comment.