From befe2606cf9b3196087241e1259e4a5fec772298 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Thu, 3 Jan 2019 12:29:28 +0000 Subject: [PATCH] [misc] use %S to print wchar_t strings where possible * Also fix some Coverity warnings --- src/drive.c | 10 ++++------ src/format.c | 7 +------ src/process.c | 4 +++- src/rufus.rc | 10 +++++----- src/vhd.c | 13 ++++--------- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/drive.c b/src/drive.c index 199a6d63..10b23037 100644 --- a/src/drive.c +++ b/src/drive.c @@ -926,7 +926,7 @@ BOOL GetDrivePartitionData(DWORD DriveIndex, char* FileSystemName, DWORD FileSys BYTE geometry[256] = {0}, layout[4096] = {0}, part_type; PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry; PDRIVE_LAYOUT_INFORMATION_EX DriveLayout = (PDRIVE_LAYOUT_INFORMATION_EX)(void*)layout; - char *volume_name, *buf, tmp[256]; + char *volume_name, *buf; if (FileSystemName == NULL) return FALSE; @@ -1046,11 +1046,9 @@ BOOL GetDrivePartitionData(DWORD DriveIndex, char* FileSystemName, DWORD FileSys DriveLayout->Gpt.MaxPartitionCount, DriveLayout->Gpt.StartingUsableOffset.QuadPart, DriveLayout->Gpt.UsableLength.QuadPart); for (i=0; iPartitionCount; i++) { SelectedDrive.nPartitions++; - tmp[0] = 0; - wchar_to_utf8_no_alloc(DriveLayout->PartitionEntry[i].Gpt.Name, tmp, sizeof(tmp)); - isUefiNtfs = (strcmp(tmp, "UEFI:NTFS") == 0); - suprintf("Partition %d%s:\r\n Type: %s\r\n Name: '%s'", i+1, isUefiNtfs ? " (UEFI:NTFS)" : "", - GuidToString(&DriveLayout->PartitionEntry[i].Gpt.PartitionType), tmp); + isUefiNtfs = (wcscmp(DriveLayout->PartitionEntry[i].Gpt.Name, L"UEFI:NTFS") == 0); + suprintf("Partition %d%s:\r\n Type: %s\r\n Name: '%S'", i+1, isUefiNtfs ? " (UEFI:NTFS)" : "", + GuidToString(&DriveLayout->PartitionEntry[i].Gpt.PartitionType), DriveLayout->PartitionEntry[i].Gpt.Name); suprintf(" ID: %s\r\n Size: %s (%" PRIi64 " bytes)\r\n Start Sector: %" PRIi64 ", Attributes: 0x%016" PRIX64, GuidToString(&DriveLayout->PartitionEntry[i].Gpt.PartitionId), SizeToHumanReadable(DriveLayout->PartitionEntry[i].PartitionLength.QuadPart, TRUE, FALSE), diff --git a/src/format.c b/src/format.c index 751ff73e..65f63330 100644 --- a/src/format.c +++ b/src/format.c @@ -73,7 +73,6 @@ static BOOL WritePBR(HANDLE hLogicalDrive); static void OutputUTF8Message(const char* src) { int len; - char *dst = NULL; wchar_t* wdst = NULL; if (src == NULL) @@ -90,13 +89,9 @@ static void OutputUTF8Message(const char* src) wdst = (wchar_t*)calloc(len+1, sizeof(wchar_t)); if ((wdst == NULL) || (MultiByteToWideChar(CP_OEMCP, 0, src, len, wdst, len+1) == 0)) goto out; - dst = wchar_to_utf8(wdst); - if (dst == NULL) - goto out; - uprintf("%s", dst); + uprintf("%S", wdst); out: - safe_free(dst); safe_free(wdst); } diff --git a/src/process.c b/src/process.c index c55bbe04..c36ab75c 100644 --- a/src/process.c +++ b/src/process.c @@ -405,6 +405,8 @@ static PWSTR GetProcessCommandLine(HANDLE hProcess) goto out; ucmdline = (UNICODE_STRING*)(pp + cmd_offset); + // In the absolute, someone could craft a process with dodgy attributes to try to cause an overflow + ucmdline->Length = min(ucmdline->Length, 512); wcmdline = (PWSTR)calloc(ucmdline->Length + 1, sizeof(WCHAR)); if (!ReadProcessMemory(hProcess, ucmdline->Buffer, wcmdline, ucmdline->Length, NULL)) { safe_free(wcmdline); @@ -433,7 +435,7 @@ static DWORD WINAPI SearchProcessThread(LPVOID param) WCHAR *wHandleName = NULL; HANDLE dupHandle = NULL; HANDLE processHandle = NULL; - BOOLEAN bFound = FALSE, bGotCmdLine, verbose = !_bQuiet; + BOOLEAN bFound = FALSE, bGotCmdLine = FALSE, verbose = !_bQuiet; ULONG access_rights = 0; DWORD size; char cmdline[MAX_PATH] = { 0 }; diff --git a/src/rufus.rc b/src/rufus.rc index 8d727819..9dc531a6 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL IDD_DIALOG DIALOGEX 12, 12, 232, 326 STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_ACCEPTFILES -CAPTION "Rufus 3.5.1435" +CAPTION "Rufus 3.5.1436" FONT 9, "Segoe UI Symbol", 400, 0, 0x0 BEGIN LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP @@ -394,8 +394,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 3,5,1435,0 - PRODUCTVERSION 3,5,1435,0 + FILEVERSION 3,5,1436,0 + PRODUCTVERSION 3,5,1436,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -413,13 +413,13 @@ BEGIN VALUE "Comments", "https://akeo.ie" VALUE "CompanyName", "Akeo Consulting" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "3.5.1435" + VALUE "FileVersion", "3.5.1436" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", "© 2011-2018 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "https://www.gnu.org/copyleft/gpl.html" VALUE "OriginalFilename", "rufus-3.5.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "3.5.1435" + VALUE "ProductVersion", "3.5.1436" END END BLOCK "VarFileInfo" diff --git a/src/vhd.c b/src/vhd.c index 6c335d75..8664eea5 100644 --- a/src/vhd.c +++ b/src/vhd.c @@ -564,7 +564,6 @@ DWORD WINAPI WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PV { PBOOL pbCancel = NULL; PWIN32_FIND_DATA pFileData; - char* str = NULL; const char* level = NULL; uint64_t size; float apply_percent; @@ -582,8 +581,7 @@ DWORD WINAPI WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PV // The amount of files processed is overwhelming (16k+ for a typical image), // and trying to display it *WILL* slow us down, so we don't. #if 0 - str = wchar_to_utf8((PWSTR)wParam); - uprintf("%s", str); + uprintf("%S", (PWSTR)wParam); PrintStatus(0, MSG_000, str); // MSG_000 is "%s" #endif if (count_files) { @@ -612,13 +610,12 @@ DWORD WINAPI WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PV } break; case WIM_MSG_FILEINFO: - str = wchar_to_utf8((PWSTR)wParam); pFileData = (PWIN32_FIND_DATA)lParam; if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - uprintf("Creating: %s", str); + uprintf("Creating: %S", (PWSTR)wParam); } else { size = (((uint64_t)pFileData->nFileSizeHigh) << 32) + pFileData->nFileSizeLow; - uprintf("Extracting: %s (%s)", str, SizeToHumanReadable(size, FALSE, FALSE)); + uprintf("Extracting: %S (%s)", (PWSTR)wParam, SizeToHumanReadable(size, FALSE, FALSE)); } break; case WIM_MSG_RETRY: @@ -632,12 +629,10 @@ DWORD WINAPI WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PV // fall through case WIM_MSG_ERROR: if (level == NULL) level = "error"; - str = wchar_to_utf8((PWSTR)wParam); SetLastError((DWORD)lParam); - uprintf("Apply %s: %s [err = %d]\n", level, str, WindowsErrorString()); + uprintf("Apply %s: %S [err = %d]\n", level, (PWSTR)wParam, WindowsErrorString()); break; } - safe_free(str); return IS_ERROR(FormatStatus)?WIM_MSG_ABORT_IMAGE:WIM_MSG_SUCCESS; }