3333#include <windows.h>
3434#include <winnt.h>
3535#include <stddef.h>
36+ #ifdef __MINGW32__
37+ // for mbstowcs_s and wcstombs_s
38+ #include <stdlib.h>
39+ #endif
3640#include <tchar.h>
3741#ifdef DEBUG_OUTPUT
3842#include <stdio.h>
@@ -1007,19 +1011,20 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry(
10071011 // using a pre-allocated array.
10081012 wchar_t _searchKeySpace [MAX_LOCAL_KEY_LENGTH + 1 ];
10091013 LPWSTR _searchKey ;
1014+ size_t _searchKeySize ;
10101015 if (searchKeyLen > MAX_LOCAL_KEY_LENGTH ) {
1011- size_t _searchKeySize = (searchKeyLen + 1 ) * sizeof (wchar_t );
1016+ _searchKeySize = (searchKeyLen + 1 ) * sizeof (wchar_t );
10121017 _searchKey = (LPWSTR ) malloc (_searchKeySize );
10131018 if (_searchKey == NULL ) {
10141019 SetLastError (ERROR_OUTOFMEMORY );
10151020 return NULL ;
10161021 }
10171022 } else {
10181023 _searchKey = & _searchKeySpace [0 ];
1024+ _searchKeySize = sizeof (_searchKeySpace );
10191025 }
10201026
1021- mbstowcs (_searchKey , key , searchKeyLen );
1022- _searchKey [searchKeyLen ] = 0 ;
1027+ mbstowcs_s (NULL , _searchKey , _searchKeySize , key , searchKeyLen );
10231028 searchKey = _searchKey ;
10241029#endif
10251030 start = 0 ;
@@ -1146,7 +1151,7 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO
11461151{
11471152 HMEMORYRSRC resource ;
11481153 PIMAGE_RESOURCE_DIR_STRING_U data ;
1149- DWORD size ;
1154+ int size ;
11501155 if (maxsize == 0 ) {
11511156 return 0 ;
11521157 }
@@ -1169,15 +1174,13 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO
11691174 }
11701175
11711176 size = data -> Length ;
1172- if (size >= (DWORD ) maxsize ) {
1173- size = maxsize ;
1174- } else {
1175- buffer [size ] = 0 ;
1177+ if (size >= maxsize ) {
1178+ size = maxsize - 1 ;
11761179 }
11771180#if defined(UNICODE )
1178- wcsncpy (buffer , data -> NameString , size );
1181+ wcsncpy_s (buffer , maxsize , data -> NameString , size );
11791182#else
1180- wcstombs ( buffer , data -> NameString , size );
1183+ wcstombs_s ( NULL , buffer , maxsize , data -> NameString , size );
11811184#endif
11821185 return size ;
11831186}
0 commit comments