--- mstools/samples/sdktools/uconvert/uconvert.c 2018/08/09 18:24:28 1.1 +++ mstools/samples/sdktools/uconvert/uconvert.c 2018/08/09 18:27:14 1.1.1.2 @@ -586,7 +586,8 @@ static TCHAR szFilter[MAX_PATH]; } - pSourceData = ManageMemory (MMALLOC, MMSOURCE, nBytesSource, pSourceData); + /* Allocate space for string, including potential UNICODE_NULL */ + pSourceData = ManageMemory (MMALLOC, MMSOURCE, nBytesSource +2, pSourceData); if (pSourceData == NULL) { CloseHandle (hFile); return 0; @@ -645,10 +646,14 @@ static TCHAR szFilter[MAX_PATH]; * try to determine if it is unicode. */ if (gTypeSource == TYPEUNKNOWN) { - if (IsUnicode (pSourceData)) + if (IsUnicode (pSourceData)) { gTypeSource = TYPEUNICODE; - else + pSourceData[nBytesSource] = 0; // UNICODE_NULL + pSourceData[nBytesSource+1] = 0; + } else { gTypeSource = TYPECODEPAGE; + pSourceData[nBytesSource] = 0; + } } SendMessage (hwnd, WMU_ADJUSTFORNEWSOURCE, 0, (LPARAM)szFile); @@ -881,13 +886,16 @@ static TCHAR szFilter[MAX_PATH]; NULL, 0, glpDefaultChar, &gUsedDefaultChar); - /* Allocate the required amount of space */ - pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, nBytesNeeded, pDestinationData); + /* Allocate the required amount of space, including trailing NULL */ + pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, nBytesNeeded +1, pDestinationData); /* Do the conversion */ nBytesDestination = WideCharToMultiByte(giDestinationCodePage, gWCFlags, (LPWSTR)pSourceData, nWCharSource, pDestinationData, nBytesNeeded, glpDefaultChar, &gUsedDefaultChar); + + /* Null terminate string. */ + pDestinationData[nBytesDestination] = 0; } @@ -898,8 +906,8 @@ static TCHAR szFilter[MAX_PATH]; nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags, pSourceData, nBytesSource, NULL, 0 ); - /* Allocate the required amount of space */ - pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, nWCharNeeded*2, pDestinationData); + /* Allocate the required amount of space, including trailing NULL */ + pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, (nWCharNeeded +1)*2, pDestinationData); /* Do the conversion */ nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags, @@ -908,6 +916,10 @@ static TCHAR szFilter[MAX_PATH]; /* MultiByteToWideChar returns # WCHAR, so multiply by 2 */ nBytesDestination = 2*nWCharNeeded ; + + /* Null terminate string. */ + pDestinationData[nBytesDestination] = 0; // UNICODE_NULL + pDestinationData[nBytesDestination+1] = 0; } else { MessageBox (hwnd, TEXT("Source type unknown.\n Specify Source Options"),MBTitle, MBFlags); return 0;