From c4cb9d03c1316f4b9fff831dfa4f6a2033bfce57 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Wed, 9 Jan 2013 21:54:28 +0000 Subject: [PATCH] [misc] fix VS2012 Code Analysis warnings * Only for files that aren't part of external dependencies * Also update copyright date --- src/badblocks.c | 3 ++- src/dos.c | 19 ++++++++++++++----- src/dos_locale.c | 7 +++++-- src/drive.c | 2 +- src/iso.c | 8 ++++---- src/msapi_utf8.h | 6 +++++- src/net.c | 6 ++++-- src/rufus.c | 26 +++++++++++++++++--------- src/rufus.h | 9 +++++++-- src/rufus.rc | 10 +++++----- src/stdio.c | 4 ++-- src/stdlg.c | 14 ++++++++++---- 12 files changed, 76 insertions(+), 38 deletions(-) diff --git a/src/badblocks.c b/src/badblocks.c index 15c4c05b..b7d27334 100644 --- a/src/badblocks.c +++ b/src/badblocks.c @@ -7,7 +7,7 @@ * * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o * Copyright 1999 by David Beattie - * Copyright 2011-2012 by Pete Batard + * Copyright 2011-2013 by Pete Batard * * This file is based on the minix file system programs fsck and mkfs * written and copyrighted by Linus Torvalds @@ -121,6 +121,7 @@ static errcode_t bb_u64_list_add(bb_u64_list bb, uint64_t blk) bb->size -= 100; return BB_ET_NO_MEMORY; } + memset(&bb->list[bb->size-100], 0, 100 * sizeof(uint64_t)); } /* diff --git a/src/dos.c b/src/dos.c index 26eaffad..fbc7ac24 100644 --- a/src/dos.c +++ b/src/dos.c @@ -2,7 +2,7 @@ * Rufus: The Reliable USB Formatting Utility * DOS boot file extraction, from the FAT12 floppy image in diskcopy.dll * (MS WinME DOS) or from the embedded FreeDOS resource files - * Copyright (c) 2011-2012 Pete Batard + * Copyright (c) 2011-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -178,7 +178,10 @@ static BOOL Patch_COMMAND_COM(HANDLE hFile) return FALSE; } SetFilePointer(hFile, 0x650c, NULL, FILE_BEGIN); - ReadFile(hFile, data, size, &size, NULL); + if (!ReadFile(hFile, data, size, &size, NULL)) { + uprintf(" could not read data\n"); + return FALSE; + } if (memcmp(data, expected, sizeof(expected)) != 0) { uprintf(" unexpected binary data\n"); return FALSE; @@ -202,7 +205,10 @@ static BOOL Patch_IO_SYS(HANDLE hFile) return FALSE; } SetFilePointer(hFile, 0x3a8, NULL, FILE_BEGIN); - ReadFile(hFile, data, size, &size, NULL); + if (!ReadFile(hFile, data, size, &size, NULL)) { + uprintf(" could not read data\n"); + return FALSE; + } if (memcmp(data, expected, sizeof(expected)) != 0) { uprintf(" unexpected binary data\n"); return FALSE; @@ -307,6 +313,7 @@ static BOOL ExtractMSDOS(const char* path) int i, j; BOOL r = TRUE; HMODULE hDLL; + HGLOBAL hRes; HRSRC hDiskImage; char locale_path[MAX_PATH]; char* extractlist[] = { "MSDOS SYS", "COMMAND COM", "IO SYS", "MODE COM", @@ -331,8 +338,10 @@ static BOOL ExtractMSDOS(const char* path) FreeLibrary(hDLL); return FALSE; } - DiskImage = (BYTE*)LockResource(LoadResource(hDLL, hDiskImage)); - if (DiskImage == NULL) { + hRes = LoadResource(hDLL, hDiskImage); + if (hRes != NULL) + DiskImage = (BYTE*)LockResource(hRes); + if ((hRes == NULL) || (DiskImage == NULL) ){ uprintf("Unable to access disk image in %s: %s\n", dllname, WindowsErrorString()); FreeLibrary(hDLL); return FALSE; diff --git a/src/dos_locale.c b/src/dos_locale.c index d8000e86..9a79a506 100644 --- a/src/dos_locale.c +++ b/src/dos_locale.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * DOS keyboard locale setup - * Copyright (c) 2011-2012 Pete Batard + * Copyright (c) 2011-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -410,7 +410,10 @@ static const char* get_kb(void) // need an KLID which GetKeyboardLayoutNameA() does return ...but only as a // string of an hex value... GetKeyboardLayoutNameA(kbid_str); - sscanf(kbid_str, "%x", &kbid); + if (sscanf(kbid_str, "%x", &kbid) == 0) { + uprintf("Could not scan keyboard layout name - falling back to US as default\n"); + kbid = 0x00000409; + } uprintf("Windows KBID 0x%08x\n", kbid); for (pass=0; pass<3; pass++) { diff --git a/src/drive.c b/src/drive.c index 411bac45..eab34bd4 100644 --- a/src/drive.c +++ b/src/drive.c @@ -180,7 +180,7 @@ BOOL GetDriveLabel(DWORD DriveIndex, char* letter, char** label) strncpy(VolumeLabel, AutorunLabel, sizeof(VolumeLabel)); safe_free(AutorunLabel); *label = VolumeLabel; - } else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, sizeof(wVolumeLabel), + } else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, ARRAYSIZE(wVolumeLabel), NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) { wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel)); *label = VolumeLabel; diff --git a/src/iso.c b/src/iso.c index 4064715b..e04090fc 100644 --- a/src/iso.c +++ b/src/iso.c @@ -1,9 +1,9 @@ /* * Rufus: The Reliable USB Formatting Utility * ISO file extraction - * Copyright (c) 2011-2012 Pete Batard + * Copyright (c) 2011-2013 Pete Batard * Based on libcdio's iso & udf samples: - * Copyright (c) 2003-2011 Rocky Bernstein + * Copyright (c) 2003-2012 Rocky Bernstein * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -185,7 +185,7 @@ static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const cha uprintf("Error allocating file name\n"); goto out; } - i_length = safe_sprintf(psz_fullpath, i_length, "%s%s/%s", psz_extract_dir, psz_path, psz_basename); + i_length = _snprintf(psz_fullpath, i_length, "%s%s/%s", psz_extract_dir, psz_path, psz_basename); if (i_length < 0) { goto out; } @@ -292,7 +292,7 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path) if ((p_iso == NULL) || (psz_path == NULL)) return 1; - i_length = safe_sprintf(psz_fullpath, sizeof(psz_fullpath), "%s%s/", psz_extract_dir, psz_path); + i_length = _snprintf(psz_fullpath, sizeof(psz_fullpath), "%s%s/", psz_extract_dir, psz_path); if (i_length < 0) return 1; psz_basename = &psz_fullpath[i_length]; diff --git a/src/msapi_utf8.h b/src/msapi_utf8.h index 05a7de50..eb7c51eb 100644 --- a/src/msapi_utf8.h +++ b/src/msapi_utf8.h @@ -2,7 +2,7 @@ * MSAPI_UTF8: Common API calls using UTF-8 strings * Compensating for what Microsoft should have done a long long time ago. * - * Copyright (c) 2010-2012 Pete Batard + * Copyright (c) 2010-2013 Pete Batard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -27,6 +27,10 @@ #include #pragma once +#if defined(_MSC_VER) +// disable VS2012 Code Analysis warnings that are intentional +#pragma warning(disable: 6387) // Don't care about bad params +#endif #ifdef __cplusplus extern "C" { diff --git a/src/net.c b/src/net.c index 9eea86d9..a09bbb50 100644 --- a/src/net.c +++ b/src/net.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * Networking functionality (web file download, check for update, etc.) - * Copyright (c) 2012 Pete Batard + * Copyright (c) 2012-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -270,6 +270,7 @@ BOOL DownloadFile(const char* url, const char* file, HWND hProgressDialog) uprintf("Unable to decode URL: %s\n", WindowsErrorString()); goto out; } + hostname[sizeof(hostname)-1] = 0; // Open an Internet session for (i=5; (i>0) && (!InternetGetConnectedState(&dwFlags, 0)); i--) { @@ -476,8 +477,9 @@ static DWORD WINAPI CheckForUpdatesThread(LPVOID param) if ((!InternetCrackUrlA(server_url, (DWORD)safe_strlen(server_url), 0, &UrlParts)) || (!InternetGetConnectedState(&dwFlags, 0))) goto out; + hostname[sizeof(hostname)-1] = 0; - _snprintf(agent, ARRAYSIZE(agent), APPLICATION_NAME "/%d.%d.%d.%d", rufus_version[0], rufus_version[1], rufus_version[2], rufus_version[3]); + safe_sprintf(agent, ARRAYSIZE(agent), APPLICATION_NAME "/%d.%d.%d.%d", rufus_version[0], rufus_version[1], rufus_version[2], rufus_version[3]); hSession = InternetOpenA(agent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hSession == NULL) goto out; diff --git a/src/rufus.c b/src/rufus.c index 85375bbf..ee504e04 100644 --- a/src/rufus.c +++ b/src/rufus.c @@ -1,6 +1,6 @@ /* * Rufus: The Reliable USB Formatting Utility - * Copyright © 2011-2012 Pete Batard + * Copyright © 2011-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -395,7 +395,7 @@ static BOOL GetDriveInfo(void) DriveLayout->PartitionEntry[i].Mbr.BootIndicator?"Yes":"No", DriveLayout->PartitionEntry[i].Mbr.RecognizedPartition?"Yes":"No", DriveLayout->PartitionEntry[i].Mbr.HiddenSectors); - tmp_pos = safe_sprintf(&tmp[tmp_pos], sizeof(tmp)-tmp_pos, "Partition %d: %s (%s)\n", + tmp_pos = _snprintf(&tmp[tmp_pos], sizeof(tmp)-tmp_pos, "Partition %d: %s (%s)\n", i+1, GetPartitionType(part_type), size_to_hr(DriveLayout->PartitionEntry[i].PartitionLength)); } } @@ -525,7 +525,7 @@ static BOOL PopulateProperties(int ComboIndex) { double HumanReadableSize; char capacity[64]; - static char *suffix[] = { "KB", "MB", "GB", "TB", "PB"}; + static char *suffix[] = { "B", "KB", "MB", "GB", "TB", "PB"}; char no_label[] = STR_NO_LABEL; int i, fs; @@ -547,7 +547,7 @@ static BOOL PopulateProperties(int ComboIndex) EnableBootOptions((fs == FS_FAT16) || (fs == FS_FAT32) || (fs == FS_NTFS)); HumanReadableSize = (double)SelectedDrive.DiskSize; - for (i=0; i= 1600) +int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) +#else int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +#endif { // int i, argc = 0, si = 0; // char** argv = NULL; @@ -1976,7 +1984,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine hMainInstance = hInstance; // Initialize COM for folder selection - CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + IGNORE_RETVAL(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)); // Some dialogs have Rich Edit controls and won't display without this if (LoadLibraryA("Riched20.dll") == NULL) { diff --git a/src/rufus.h b/src/rufus.h index 68a0e5f2..e4cbca9b 100644 --- a/src/rufus.h +++ b/src/rufus.h @@ -1,6 +1,6 @@ /* * Rufus: The Reliable USB Formatting Utility - * Copyright © 2011-2012 Pete Batard + * Copyright © 2011-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +19,11 @@ #include // for DISK_GEOMETRY #include +#if defined(_MSC_VER) +// Disable some VS2012 Code Analysis warnings +#pragma warning(disable: 28159) // VS2012 wants us to use GetTickCount64(), but it's not available on XP +#endif + #pragma once /* Program options */ @@ -63,7 +68,7 @@ #define safe_strnicmp(str1, str2, count) _strnicmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2), count) #define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0) #define safe_unlockclose(h) do {if (h != INVALID_HANDLE_VALUE) {UnlockDrive(h); CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0) -#define safe_sprintf _snprintf +#define safe_sprintf(dst, count, ...) do {_snprintf(dst, count, __VA_ARGS__); (dst)[(count)-1] = 0; } while(0) #define safe_strlen(str) ((((char*)str)==NULL)?0:strlen(str)) #define safe_strdup _strdup #if defined(_MSC_VER) diff --git a/src/rufus.rc b/src/rufus.rc index 902b33e4..571c2f09 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -30,7 +30,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL IDD_DIALOG DIALOGEX 12, 12, 206, 316 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_APPWINDOW -CAPTION "Rufus v1.3.0.215" +CAPTION "Rufus v1.3.0.216" FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN DEFPUSHBUTTON "Start",IDC_START,94,278,50,14 @@ -274,8 +274,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,3,0,215 - PRODUCTVERSION 1,3,0,215 + FILEVERSION 1,3,0,216 + PRODUCTVERSION 1,3,0,216 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -292,13 +292,13 @@ BEGIN BEGIN VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "1.3.0.215" + VALUE "FileVersion", "1.3.0.216" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", "(c) 2011-2012 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "OriginalFilename", "rufus.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "1.3.0.215" + VALUE "ProductVersion", "1.3.0.216" END END BLOCK "VarFileInfo" diff --git a/src/stdio.c b/src/stdio.c index b9ff1bff..717dfc82 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -1,7 +1,7 @@ /* * Rufus: The Reliable USB Formatting Utility * Standard I/O Routines (logging, status, etc.) - * Copyright (c) 2011-2012 Pete Batard + * Copyright (c) 2011-2013 Pete Batard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -107,7 +107,7 @@ void DumpBufferHex(void *buf, size_t size) */ const char *WindowsErrorString(void) { -static char err_string[256]; +static char err_string[256] = {0}; DWORD size; DWORD error_code, format_error; diff --git a/src/stdlg.c b/src/stdlg.c index fef84fda..b482ae6d 100644 --- a/src/stdlg.c +++ b/src/stdlg.c @@ -1,9 +1,9 @@ /* * Rufus: The Reliable USB Formatting Utility * Standard Dialog Routines (Browse for folder, About, etc) - * Copyright © 2011-2012 Pete Batard + * Copyright © 2011-2013 Pete Batard * - * Based on zadig_stdlg.c, part of libwdi: http://libwdi.sf.net + * Based on zadig_stdlg.c, part of libwdi: http://libwdi.akeo.ie * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -114,12 +114,15 @@ void StrArrayCreate(StrArray* arr, size_t initial_size) void StrArrayAdd(StrArray* arr, const char* str) { + char** old_table; if ((arr == NULL) || (arr->Table == NULL)) return; if (arr->Index == arr->Max) { arr->Max *= 2; + old_table = arr->Table; arr->Table = (char**)realloc(arr->Table, arr->Max*sizeof(char*)); if (arr->Table == NULL) { + free(old_table); uprintf("Could not reallocate string array\n"); return; } @@ -700,6 +703,7 @@ INT_PTR CALLBACK AboutCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lP tr.chrg.cpMin = enl->chrg.cpMin; tr.chrg.cpMax = enl->chrg.cpMax; SendMessageW(enl->nmhdr.hwndFrom, EM_GETTEXTRANGE, 0, (LPARAM)&tr); + wUrl[ARRAYSIZE(wUrl)-1] = 0; ShellExecuteW(hDlg, L"open", wUrl, NULL, NULL, SW_SHOWNORMAL); } break; @@ -970,7 +974,8 @@ void DestroyAllTooltips(void) /* Determine if a Windows is being displayed or not */ BOOL IsShown(HWND hDlg) { - WINDOWPLACEMENT placement; + WINDOWPLACEMENT placement = {0}; + placement.length = sizeof(WINDOWPLACEMENT); if (!GetWindowPlacement(hDlg, &placement)) return FALSE; switch (placement.showCmd) { @@ -1282,7 +1287,7 @@ INT_PTR CALLBACK NewVersionCallback(HWND hDlg, UINT message, WPARAM wParam, LPAR update.version[0], update.version[1], update.version[2], update.version[3]); SetWindowTextA(GetDlgItem(hDlg, IDC_LATEST_VERSION), tmp); SetWindowTextA(GetDlgItem(hDlg, IDC_DOWNLOAD_URL), update.download_url); - SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), PBM_SETRANGE, 0, MAX_PROGRESS<<16); + SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), PBM_SETRANGE, 0, (MAX_PROGRESS<<16) & 0xFFFF0000); if (update.download_url == NULL) EnableWindow(GetDlgItem(hDlg, IDC_DOWNLOAD), FALSE); break; @@ -1301,6 +1306,7 @@ INT_PTR CALLBACK NewVersionCallback(HWND hDlg, UINT message, WPARAM wParam, LPAR tr.chrg.cpMin = enl->chrg.cpMin; tr.chrg.cpMax = enl->chrg.cpMax; SendMessageW(enl->nmhdr.hwndFrom, EM_GETTEXTRANGE, 0, (LPARAM)&tr); + wUrl[ARRAYSIZE(wUrl)-1] = 0; ShellExecuteW(hDlg, L"open", wUrl, NULL, NULL, SW_SHOWNORMAL); } break;