From c220db01281564bf5b50575ee7e24b38e45f5050 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 14 Jul 2014 17:41:09 +0200 Subject: Static Code Analysis : Generalize the use of Safe String functions. Add some NULL pointer checks. Avoid false-positive detection in AppendMenu (MF_SEPARATOR) calls by setting the last parameter to "" instead of NULL. --- src/Common/BootEncryption.cpp | 32 +++-- src/Common/Cmdline.c | 11 +- src/Common/Format.c | 14 +- src/Common/Keyfiles.c | 109 +++++++++------- src/Common/Language.c | 50 +++++--- src/Common/Progress.c | 32 ++--- src/Common/Random.c | 7 +- src/Common/Tcdefs.h | 2 +- src/Common/Volumes.c | 14 +- src/Format/InPlace.c | 34 ++--- src/Format/Tcformat.c | 188 +++++++++++++-------------- src/Mount/Favorites.cpp | 4 +- src/Mount/Hotkeys.c | 94 +++++++------- src/Mount/Mount.c | 290 +++++++++++++++++++++++------------------- src/Mount/Mount.h | 4 +- 15 files changed, 490 insertions(+), 395 deletions(-) (limited to 'src') diff --git a/src/Common/BootEncryption.cpp b/src/Common/BootEncryption.cpp index 1a467eed..0fec2878 100644 --- a/src/Common/BootEncryption.cpp +++ b/src/Common/BootEncryption.cpp @@ -32,6 +32,8 @@ #include "Mount/MainCom.h" #endif +#include + namespace VeraCrypt { #if !defined (SETUP) @@ -604,7 +606,7 @@ namespace VeraCrypt GetSystemDriveConfiguration(); ProbeRealDriveSizeRequest request; - _snwprintf (request.DeviceName, array_capacity (request.DeviceName), L"%hs", DriveConfig.DrivePartition.DevicePath.c_str()); + StringCbPrintfW (request.DeviceName, sizeof (request.DeviceName), L"%hs", DriveConfig.DrivePartition.DevicePath.c_str()); CallDriver (TC_IOCTL_PROBE_REAL_DRIVE_SIZE, &request, sizeof (request), &request, sizeof (request)); DriveConfig.DrivePartition.Info.PartitionLength = request.RealDriveSize; @@ -633,7 +635,7 @@ namespace VeraCrypt partPath << "\\Device\\Harddisk" << driveNumber << "\\Partition" << partNumber; DISK_PARTITION_INFO_STRUCT diskPartInfo; - _snwprintf (diskPartInfo.deviceName, array_capacity (diskPartInfo.deviceName), L"%hs", partPath.str().c_str()); + StringCbPrintfW (diskPartInfo.deviceName, sizeof (diskPartInfo.deviceName), L"%hs", partPath.str().c_str()); try { @@ -663,7 +665,7 @@ namespace VeraCrypt // Volume ID wchar_t volumePath[TC_MAX_PATH]; - if (ResolveSymbolicLink ((wchar_t *) ws.str().c_str(), volumePath)) + if (ResolveSymbolicLink ((wchar_t *) ws.str().c_str(), volumePath, sizeof(volumePath))) { wchar_t volumeName[TC_MAX_PATH]; HANDLE fh = FindFirstVolumeW (volumeName, array_capacity (volumeName)); @@ -742,8 +744,8 @@ namespace VeraCrypt memset (&openTestStruct, 0, sizeof (openTestStruct)); DWORD dwResult; - strcpy ((char *) &openTestStruct.wszFileName[0], devicePath); - ToUNICODE ((char *) &openTestStruct.wszFileName[0]); + StringCbCopyA ((char *) &openTestStruct.wszFileName[0], sizeof(openTestStruct.wszFileName),devicePath); + ToUNICODE ((char *) &openTestStruct.wszFileName[0], sizeof(openTestStruct.wszFileName)); openTestStruct.bDetectTCBootLoader = TRUE; @@ -844,7 +846,7 @@ namespace VeraCrypt bool BootEncryption::SystemDriveIsDynamic () { GetSystemDriveConfigurationRequest request; - _snwprintf (request.DevicePath, array_capacity (request.DevicePath), L"%hs", GetSystemDriveConfiguration().DeviceKernelPath.c_str()); + StringCbPrintfW (request.DevicePath, sizeof (request.DevicePath), L"%hs", GetSystemDriveConfiguration().DeviceKernelPath.c_str()); CallDriver (TC_IOCTL_GET_SYSTEM_DRIVE_CONFIG, &request, sizeof (request), &request, sizeof (request)); return request.DriveIsDynamic ? true : false; @@ -1095,7 +1097,7 @@ namespace VeraCrypt throw ParameterIncorrect (SRC_POS); GetSystemDriveConfigurationRequest request; - _snwprintf (request.DevicePath, array_capacity (request.DevicePath), L"%hs", GetSystemDriveConfiguration().DeviceKernelPath.c_str()); + StringCbPrintfW (request.DevicePath, sizeof (request.DevicePath), L"%hs", GetSystemDriveConfiguration().DeviceKernelPath.c_str()); try { @@ -1402,8 +1404,10 @@ namespace VeraCrypt memset (image, 0, RescueIsoImageSize); // Primary volume descriptor - strcpy ((char *)image + 0x8000, "\001CD001\001"); - strcpy ((char *)image + 0x7fff + 41, "VeraCrypt Rescue Disk "); + const char* szPrimVolDesc = "\001CD001\001"; + const char* szPrimVolLabel = "VeraCrypt Rescue Disk "; + memcpy (image + 0x8000, szPrimVolDesc, strlen(szPrimVolDesc) + 1); + memcpy (image + 0x7fff + 41, szPrimVolLabel, strlen(szPrimVolLabel) + 1); *(uint32 *) (image + 0x7fff + 81) = RescueIsoImageSize / 2048; *(uint32 *) (image + 0x7fff + 85) = BE32 (RescueIsoImageSize / 2048); image[0x7fff + 121] = 1; @@ -1420,11 +1424,13 @@ namespace VeraCrypt image[0x7fff + 159] = 0x18; // Boot record volume descriptor - strcpy ((char *)image + 0x8801, "CD001\001EL TORITO SPECIFICATION"); + const char* szBootRecDesc = "CD001\001EL TORITO SPECIFICATION"; + memcpy (image + 0x8801, szBootRecDesc, strlen(szBootRecDesc) + 1); image[0x8800 + 0x47] = 0x19; // Volume descriptor set terminator - strcpy ((char *)image + 0x9000, "\377CD001\001"); + const char* szVolDescTerm = "\377CD001\001"; + memcpy (image + 0x9000, szVolDescTerm, strlen(szVolDescTerm) + 1); // Path table image[0xA000 + 0] = 1; @@ -1722,7 +1728,7 @@ namespace VeraCrypt DWORD size = sizeof (regKeyBuf) - strSize; // SetupInstallFromInfSection() does not support prepending of values so we have to modify the registry directly - strncpy ((char *) regKeyBuf, filter.c_str(), sizeof (regKeyBuf)); + StringCbCopyA ((char *) regKeyBuf, sizeof(regKeyBuf), filter.c_str()); if (RegQueryValueEx (regKey, filterReg.c_str(), NULL, NULL, regKeyBuf + strSize, &size) != ERROR_SUCCESS) size = 1; @@ -2318,7 +2324,7 @@ namespace VeraCrypt void BootEncryption::RestrictPagingFilesToSystemPartition () { char pagingFiles[128]; - strncpy (pagingFiles, "X:\\pagefile.sys 0 0", sizeof (pagingFiles)); + StringCbCopyA (pagingFiles, sizeof(pagingFiles), "X:\\pagefile.sys 0 0"); pagingFiles[0] = GetWindowsDirectory()[0]; throw_sys_if (!WriteLocalMachineRegistryMultiString ("System\\CurrentControlSet\\Control\\Session Manager\\Memory Management", "PagingFiles", pagingFiles, strlen (pagingFiles) + 2)); diff --git a/src/Common/Cmdline.c b/src/Common/Cmdline.c index 35507c2b..f1f9a8fc 100644 --- a/src/Common/Cmdline.c +++ b/src/Common/Cmdline.c @@ -20,6 +20,7 @@ #include "Apidrvr.h" #include "Dlgcode.h" #include "Language.h" +#include /* Except in response to the WM_INITDIALOG message, the dialog box procedure should return nonzero if it processes the message, and zero if it does @@ -44,13 +45,13 @@ BOOL CALLBACK CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM *tmp = 0; - strcpy (tmp, "Command line options:\n\n"); + StringCbCopyA (tmp, 8192, "Command line options:\n\n"); for (i = 0; i < as->arg_cnt; i ++) { if (!as->args[i].Internal) { - sprintf(tmp2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name); - strcat(tmp,tmp2); + StringCchPrintf(tmp2, MAX_PATH * 2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name); + StringCchCat(tmp, 8192, tmp2); } } @@ -220,7 +221,7 @@ int GetArgumentValue (char **lpszCommandLineArgs, int nArgPos, int *nArgIdx, { /* Handles the case of no space between parameter code and value */ - strncpy (lpszValue, &lpszCommandLineArgs[*nArgIdx][nArgPos], nValueSize); + StringCbCopyA (lpszValue, nValueSize, &lpszCommandLineArgs[*nArgIdx][nArgPos]); lpszValue[nValueSize - 1] = 0; return HAS_ARGUMENT; } @@ -231,7 +232,7 @@ int GetArgumentValue (char **lpszCommandLineArgs, int nArgPos, int *nArgIdx, { /* Handles the case of space between parameter code and value */ - strncpy (lpszValue, &lpszCommandLineArgs[*nArgIdx + 1][x], nValueSize); + StringCbCopyA (lpszValue, nValueSize, &lpszCommandLineArgs[*nArgIdx + 1][x]); lpszValue[nValueSize - 1] = 0; (*nArgIdx)++; return HAS_ARGUMENT; diff --git a/src/Common/Format.c b/src/Common/Format.c index 25f20acd..ad6be026 100644 --- a/src/Common/Format.c +++ b/src/Common/Format.c @@ -29,6 +29,8 @@ #include "Format/FormatCom.h" #include "Format/Tcformat.h" +#include + int FormatWriteBufferSize = 1024 * 1024; static uint32 FormatSectorSize = 0; @@ -129,8 +131,8 @@ int TCFormatVolume (volatile FORMAT_VOL_PARAMETERS *volParams) if (volParams->bDevice) { - strcpy ((char *)deviceName, volParams->volumePath); - ToUNICODE ((char *)deviceName); + StringCbCopyA ((char *)deviceName, sizeof(deviceName), volParams->volumePath); + ToUNICODE ((char *)deviceName, sizeof(deviceName)); driveLetter = GetDiskDeviceDriveLetter (deviceName); } @@ -170,7 +172,7 @@ begin_format: DWORD dwResult; int nPass; - if (FakeDosNameForDevice (volParams->volumePath, dosDev, devName, FALSE) != 0) + if (FakeDosNameForDevice (volParams->volumePath, dosDev, sizeof(dosDev), devName, sizeof(devName), FALSE) != 0) return ERR_OS_ERROR; if (IsDeviceMounted (devName)) @@ -803,10 +805,10 @@ BOOL FormatNtfs (int driveNo, int clusterSize) if (GetSystemDirectory (dllPath, MAX_PATH)) { - strcat(dllPath, "\\fmifs.dll"); + StringCbCatA(dllPath, sizeof(dllPath), "\\fmifs.dll"); } else - strcpy(dllPath, "C:\\Windows\\System32\\fmifs.dll"); + StringCbCopyA(dllPath, sizeof(dllPath), "C:\\Windows\\System32\\fmifs.dll"); hModule = LoadLibrary (dllPath); @@ -819,7 +821,7 @@ BOOL FormatNtfs (int driveNo, int clusterSize) return FALSE; } - wcscat (dir, L":\\"); + StringCbCatW (dir, sizeof(dir), L":\\"); FormatExResult = FALSE; diff --git a/src/Common/Keyfiles.c b/src/Common/Keyfiles.c index e7e55ca5..4a6baf46 100644 --- a/src/Common/Keyfiles.c +++ b/src/Common/Keyfiles.c @@ -23,11 +23,12 @@ #include "Platform/Finally.h" #include "Platform/ForEach.h" +#include + using namespace VeraCrypt; #define stat _stat #define S_IFDIR _S_IFDIR -#define snprintf _snprintf BOOL HiddenFilesPresentInKeyfilePath = FALSE; @@ -97,13 +98,16 @@ void KeyFileRemoveAll (KeyFile **firstKeyFile) KeyFile *KeyFileClone (KeyFile *keyFile) { - KeyFile *clone; + KeyFile *clone = NULL; if (keyFile == NULL) return NULL; clone = (KeyFile *) malloc (sizeof (KeyFile)); - strcpy (clone->FileName, keyFile->FileName); - clone->Next = NULL; + if (clone) + { + StringCbCopyA (clone->FileName, sizeof(clone->FileName), keyFile->FileName); + clone->Next = NULL; + } return clone; } @@ -298,7 +302,7 @@ BOOL KeyFilesApply (Password *password, KeyFile *firstKeyFile) /* Find and process all keyfiles in the directory */ int keyfileCount = 0; - snprintf (searchPath, sizeof (searchPath), "%s\\*.*", kf->FileName); + StringCbPrintfA (searchPath, sizeof (searchPath), "%s\\*.*", kf->FileName); if ((searchHandle = _findfirst (searchPath, &fBuf)) == -1) { handleWin32Error (MainDlg); @@ -311,7 +315,7 @@ BOOL KeyFilesApply (Password *password, KeyFile *firstKeyFile) { WIN32_FILE_ATTRIBUTE_DATA fileAttributes; - snprintf (kfSub->FileName, sizeof(kfSub->FileName), "%s%c%s", kf->FileName, + StringCbPrintfA (kfSub->FileName, sizeof(kfSub->FileName), "%s%c%s", kf->FileName, '\\', fBuf.name ); @@ -462,18 +466,21 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa if (lw == IDC_KEYADD) { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, bHistory)) + if (kf) { - do + if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, sizeof(kf->FileName),bHistory)) { - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - LoadKeyList (hwndDlg, param->FirstKeyFile); + do + { + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + LoadKeyList (hwndDlg, param->FirstKeyFile); - kf = (KeyFile *) malloc (sizeof (KeyFile)); - } while (SelectMultipleFilesNext (kf->FileName)); - } + kf = (KeyFile *) malloc (sizeof (KeyFile)); + } while (SelectMultipleFilesNext (kf->FileName, sizeof(kf->FileName))); + } - free (kf); + free (kf); + } return 1; } @@ -501,10 +508,13 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); + if (kf) + { + strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - LoadKeyList (hwndDlg, param->FirstKeyFile); + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + LoadKeyList (hwndDlg, param->FirstKeyFile); + } } } @@ -574,9 +584,12 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa while (count-- > 0) { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - LoadKeyList (hwndDlg, param->FirstKeyFile); + if (kf) + { + DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + LoadKeyList (hwndDlg, param->FirstKeyFile); + } } DragFinish (hdrop); @@ -614,6 +627,8 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *param) { HMENU popup = CreatePopupMenu (); + if (!popup) + return FALSE; int sel; BOOL status = FALSE; @@ -628,35 +643,40 @@ BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *par case IDM_KEYFILES_POPUP_ADD_FILES: { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, bHistory)) + if (kf) { - do + if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, sizeof(kf->FileName),bHistory)) { - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - kf = (KeyFile *) malloc (sizeof (KeyFile)); - } while (SelectMultipleFilesNext (kf->FileName)); + do + { + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + kf = (KeyFile *) malloc (sizeof (KeyFile)); + } while (SelectMultipleFilesNext (kf->FileName, sizeof(kf->FileName))); - param->EnableKeyFiles = TRUE; - status = TRUE; - } + param->EnableKeyFiles = TRUE; + status = TRUE; + } - free (kf); + free (kf); + } } break; case IDM_KEYFILES_POPUP_ADD_DIR: { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - - if (BrowseDirectories (hwndDlg,"SELECT_KEYFILE_PATH", kf->FileName)) + if (kf) { - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - param->EnableKeyFiles = TRUE; - status = TRUE; - } - else - { - free (kf); + if (BrowseDirectories (hwndDlg,"SELECT_KEYFILE_PATH", kf->FileName)) + { + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + param->EnableKeyFiles = TRUE; + status = TRUE; + } + else + { + free (kf); + } } } break; @@ -669,11 +689,14 @@ BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *par foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); - - param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); - param->EnableKeyFiles = TRUE; - status = TRUE; + if (kf) + { + strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); + + param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); + param->EnableKeyFiles = TRUE; + status = TRUE; + } } } } diff --git a/src/Common/Language.c b/src/Common/Language.c index b146f820..3ec95dba 100644 --- a/src/Common/Language.c +++ b/src/Common/Language.c @@ -26,6 +26,8 @@ #include "../Setup/Resource.h" #endif +#include + BOOL LocalizationActive; int LocalizationSerialNo; @@ -68,8 +70,9 @@ static char *MapNextLanguageFile () GetModuleFileNameW (NULL, f, sizeof (f) / sizeof (f[0])); t = wcsrchr (f, L'\\'); if (t == NULL) return NULL; - - wcscpy (t, L"\\Language*.xml"); + + *t = 0; + StringCbCatW (f, sizeof(f), L"\\Language*.xml"); LanguageFileFindHandle = FindFirstFileW (f, &find); } @@ -88,14 +91,29 @@ static char *MapNextLanguageFile () GetModuleFileNameW (NULL, f, sizeof (f) / sizeof(f[0])); t = wcsrchr (f, L'\\'); - wcscpy (t + 1, find.cFileName); + if (t == NULL) + { + free(LanguageFileBuffer); + return NULL; + } + + t[1] = 0; + StringCbCatW (f, sizeof(f),find.cFileName); file = CreateFileW (f, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); - if (file == INVALID_HANDLE_VALUE) return NULL; + if (file == INVALID_HANDLE_VALUE) + { + free(LanguageFileBuffer); + return NULL; + } ReadFile (file, LanguageFileBuffer, find.nFileSizeLow, &read, NULL); CloseHandle (file); - if (read != find.nFileSizeLow) return NULL; + if (read != find.nFileSizeLow) + { + free(LanguageFileBuffer); + return NULL; + } return LanguageFileBuffer; } @@ -130,7 +148,7 @@ BOOL LoadLanguageFile () ClearDictionaryPool (); if (PreferredLangId[0] != 0) - strcpy (langId, PreferredLangId); + StringCbCopyA (langId, sizeof(langId), PreferredLangId); // Parse all available language files until preferred language is found for (res = MapFirstLanguageFile (); res != NULL; res = MapNextLanguageFile ()) @@ -147,7 +165,7 @@ BOOL LoadLanguageFile () if (defaultLangParsed && strcmp (attr, VERSION_STRING) && strcmp (attr, "DEBUG")) { wchar_t m[2048]; - swprintf (m, L"The installed language pack is incompatible with this version of VeraCrypt (the language pack is for VeraCrypt %hs). A newer version may be available at www.idrix.fr.\n\nTo prevent this message from being displayed, do any of the following:\n\n- Select 'Settings' > 'Language'; then select 'English' and click 'OK'.\n\n- Remove or replace the language pack with a compatible version (the language pack may reside e.g. in 'C:\\Program Files\\VeraCrypt' or '%%LOCALAPPDATA%%\\VirtualStore\\Program Files\\VeraCrypt', etc.)", attr); + StringCbPrintfW (m, sizeof(m), L"The installed language pack is incompatible with this version of VeraCrypt (the language pack is for VeraCrypt %hs). A newer version may be available at www.idrix.fr.\n\nTo prevent this message from being displayed, do any of the following:\n\n- Select 'Settings' > 'Language'; then select 'English' and click 'OK'.\n\n- Remove or replace the language pack with a compatible version (the language pack may reside e.g. in 'C:\\Program Files\\VeraCrypt' or '%%LOCALAPPDATA%%\\VirtualStore\\Program Files\\VeraCrypt', etc.)", attr); MessageBoxW (NULL, m, L"VeraCrypt", MB_ICONERROR); continue; } @@ -189,7 +207,7 @@ BOOL LoadLanguageFile () XmlGetAttributeText (xml, "size", attr, sizeof (attr)); sscanf (attr, "%d", &font.Size); - strcpy (attr, "font_"); + StringCbCopyA (attr, sizeof(attr), "font_"); XmlGetAttributeText (xml, "class", attr + 5, sizeof (attr) - 5); AddDictionaryEntry ( AddPoolData ((void *) attr, strlen (attr) + 1), 0, @@ -375,13 +393,13 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa // Language pack version if (!ActiveLangPackVersion[0] || memcmp (ActiveLangPackVersion, "0.0.0", 5) == 0) { - swprintf (szVers, GetString("LANG_PACK_VERSION"), L"--"); + StringCbPrintfW (szVers, sizeof(szVers), GetString("LANG_PACK_VERSION"), L"--"); } else { nLen = MultiByteToWideChar (CP_UTF8, 0, ActiveLangPackVersion, -1, wversion, sizeof (wversion) / sizeof(wversion[0])); if (nLen != 0 && nLen != ERROR_NO_UNICODE_TRANSLATION) - swprintf (szVers, GetString("LANG_PACK_VERSION"), wversion); + StringCbPrintfW (szVers, sizeof(szVers),GetString("LANG_PACK_VERSION"), wversion); } SetWindowTextW (GetDlgItem (hwndDlg, IDC_LANGPACK_VERSION), szVers); @@ -394,7 +412,7 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa } } - strcpy (lastLangId, attr); + StringCbCopyA (lastLangId, sizeof(lastLangId),attr); langCount++; } } @@ -410,7 +428,7 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa EndDialog (hwndDlg, IDCANCEL); if (langCount == 2) - strcpy (PreferredLangId, lastLangId); + StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), lastLangId); EndDialog (hwndDlg, IDOK); } @@ -446,7 +464,7 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa } if (SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_GETCOUNT, 0, 0) > 1) - strcpy (PreferredLangId, l); + StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), l); } } @@ -465,7 +483,7 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa char tmpstr [256]; if (strlen (ActiveLangPackVersion) > 0 && strlen (GetPreferredLangId()) > 0) - sprintf (tmpstr, "&langpackversion=%s&lang=%s", ActiveLangPackVersion, GetPreferredLangId()); + StringCbPrintfA (tmpstr, sizeof(tmpstr), "&langpackversion=%s&lang=%s", ActiveLangPackVersion, GetPreferredLangId()); else tmpstr[0] = 0; @@ -488,7 +506,7 @@ char *GetPreferredLangId () void SetPreferredLangId (char *langId) { - strncpy (PreferredLangId, langId, 5); + StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), langId); } @@ -503,7 +521,7 @@ wchar_t *GetString (const char *stringId) WCHAR *str = (WCHAR *) GetDictionaryValue (stringId); if (str != NULL) return str; - wsprintfW (UnknownString, UNKNOWN_STRING_ID L"%hs" UNKNOWN_STRING_ID, stringId); + StringCbPrintfW (UnknownString, sizeof(UnknownString), UNKNOWN_STRING_ID L"%hs" UNKNOWN_STRING_ID, stringId); return UnknownString; } diff --git a/src/Common/Progress.c b/src/Common/Progress.c index afa6f645..89697a62 100644 --- a/src/Common/Progress.c +++ b/src/Common/Progress.c @@ -17,6 +17,8 @@ #include "../Format/FormatCom.h" #include "../Format/resource.h" +#include + static ULONG prevTime, startTime; static __int64 TotalSize; static __int64 resumedPointBytesDone; @@ -74,31 +76,31 @@ BOOL UpdateProgressBarProc (__int64 byteOffset) double perc = (double) (100.0 * (bProgressBarReverse ? ((double) (TotalSize - byteOffset)) : ((double) byteOffset)) / (TotalSize == 0 ? 0.0001 : ((double) TotalSize))); if (perc > 99.999999999) - wcscpy (text, GetString ("PROCESSED_PORTION_100_PERCENT")); + StringCbCopyW (text,sizeof(text), GetString ("PROCESSED_PORTION_100_PERCENT")); else - _snwprintf (text, sizeof text/2, GetString ("PROCESSED_PORTION_X_PERCENT"), perc); + StringCbPrintfW (text, sizeof text, GetString ("PROCESSED_PORTION_X_PERCENT"), perc); - wcscat (speed, L" "); + StringCbCatW (speed, sizeof(speed), L" "); } else { - GetSizeString (bytesDone, text); + GetSizeString (bytesDone, text, sizeof(text)); if (bytesDone < (unsigned __int64) BYTES_PER_MB * 1000000) - swprintf(text, L"%I64d %s ", bytesDone / BYTES_PER_MB, GetString ("MB")); + StringCbPrintfW(text, sizeof(text), L"%I64d %s ", bytesDone / BYTES_PER_MB, GetString ("MB")); else if (bytesDone < (unsigned __int64) BYTES_PER_GB * 1000000) - swprintf(text, L"%I64d %s ", bytesDone / BYTES_PER_GB, GetString ("GB")); + StringCbPrintfW(text, sizeof(text), L"%I64d %s ", bytesDone / BYTES_PER_GB, GetString ("GB")); else if (bytesDone < (unsigned __int64) BYTES_PER_TB * 1000000) - swprintf(text, L"%I64d %s ", bytesDone / BYTES_PER_TB, GetString ("TB")); + StringCbPrintfW(text, sizeof(text), L"%I64d %s ", bytesDone / BYTES_PER_TB, GetString ("TB")); else - swprintf(text, L"%I64d %s ", bytesDone / BYTES_PER_PB, GetString ("PB")); + StringCbPrintfW(text, sizeof(text), L"%I64d %s ", bytesDone / BYTES_PER_PB, GetString ("PB")); } SetWindowTextW (GetDlgItem (hCurPage, IDC_BYTESWRITTEN), text); if (!bShowStatus) { - GetSpeedString (bRWThroughput ? bytesPerSec*2 : bytesPerSec, speed); - wcscat (speed, L" "); + GetSpeedString (bRWThroughput ? bytesPerSec*2 : bytesPerSec, speed, sizeof(speed)); + StringCbCatW (speed, sizeof(speed), L" "); SetWindowTextW (GetDlgItem (hCurPage, IDC_WRITESPEED), speed); } @@ -107,15 +109,15 @@ BOOL UpdateProgressBarProc (__int64 byteOffset) int64 sec = (int64) ((bProgressBarReverse ? byteOffset : (TotalSize - byteOffset)) / (bytesPerSec == 0 ? 0.001 : bytesPerSec)); if (bytesPerSec == 0 || sec > 60 * 60 * 24 * 999) - swprintf (text, L"%s ", GetString ("NOT_APPLICABLE_OR_NOT_AVAILABLE")); + StringCbPrintfW (text, sizeof(text), L"%s ", GetString ("NOT_APPLICABLE_OR_NOT_AVAILABLE")); else if (sec >= 60 * 60 * 24 * 2) - swprintf (text, L"%I64d %s ", sec / (60 * 24 * 60), days); + StringCbPrintfW (text, sizeof(text), L"%I64d %s ", sec / (60 * 24 * 60), days); else if (sec >= 120 * 60) - swprintf (text, L"%I64d %s ", sec / (60 * 60), hours); + StringCbPrintfW (text, sizeof(text), L"%I64d %s ", sec / (60 * 60), hours); else if (sec >= 120) - swprintf (text, L"%I64d %s ", sec / 60, minutes); + StringCbPrintfW (text, sizeof(text), L"%I64d %s ", sec / 60, minutes); else - swprintf (text, L"%I64d %s ", sec, seconds); + StringCbPrintfW (text, sizeof(text), L"%I64d %s ", sec, seconds); SetWindowTextW (GetDlgItem (hCurPage, IDC_TIMEREMAIN), text); } diff --git a/src/Common/Random.c b/src/Common/Random.c index c897e3b7..445e8f24 100644 --- a/src/Common/Random.c +++ b/src/Common/Random.c @@ -12,6 +12,7 @@ #include "Tcdefs.h" #include "Crc.h" #include "Random.h" +#include static unsigned __int8 buffer[RNG_POOL_SIZE]; static unsigned char *pRandPool = NULL; @@ -576,10 +577,10 @@ BOOL SlowPoll (void) char dllPath[MAX_PATH]; if (GetSystemDirectory (dllPath, MAX_PATH)) { - strcat(dllPath, "\\NETAPI32.DLL"); + StringCbCatA(dllPath, sizeof(dllPath), "\\NETAPI32.DLL"); } else - strcpy(dllPath, "C:\\Windows\\System32\\NETAPI32.DLL"); + StringCbCopyA(dllPath, sizeof(dllPath), "C:\\Windows\\System32\\NETAPI32.DLL"); hNetAPI32 = LoadLibrary (dllPath); if (hNetAPI32 != NULL) @@ -630,7 +631,7 @@ BOOL SlowPoll (void) char szDevice[24]; /* Check whether we can access this device */ - sprintf (szDevice, "\\\\.\\PhysicalDrive%d", nDrive); + StringCbPrintfA (szDevice, sizeof(szDevice), "\\\\.\\PhysicalDrive%d", nDrive); hDevice = CreateFile (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDevice == INVALID_HANDLE_VALUE) diff --git a/src/Common/Tcdefs.h b/src/Common/Tcdefs.h index 0bf41d79..423eed27 100644 --- a/src/Common/Tcdefs.h +++ b/src/Common/Tcdefs.h @@ -197,7 +197,7 @@ typedef int BOOL; # ifdef DEVICE_DRIVER # define trace_msg Dump # elif defined (_WIN32) -# define trace_msg(...) do { char msg[2048]; _snprintf (msg, sizeof (msg), __VA_ARGS__); OutputDebugString (msg); } while (0) +# define trace_msg(...) do { char msg[2048]; StringCbPrintfA (msg, sizeof (msg), __VA_ARGS__); OutputDebugString (msg); } while (0) # endif # define trace_point trace_msg (__FUNCTION__ ":" TC_TO_STRING(__LINE__) "\n") # else diff --git a/src/Common/Volumes.c b/src/Common/Volumes.c index 506a4d04..15ee8fe6 100644 --- a/src/Common/Volumes.c +++ b/src/Common/Volumes.c @@ -33,6 +33,9 @@ #include "Volumes.h" #include "Pkcs5.h" +#ifdef _WIN32 +#include +#endif /* Volume header v5 structure (used since TrueCrypt 7.0): */ // @@ -187,6 +190,9 @@ int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, PCR } else { + if (!retInfo) + return ERR_PARAMETER_INCORRECT; + cryptoInfo = *retInfo = crypto_open (); if (cryptoInfo == NULL) return ERR_OUTOFMEMORY; @@ -934,16 +940,16 @@ int CreateVolumeHeaderInMemory (BOOL bBoot, char *header, int ea, int mode, Pass for (i = 0; i < j; i++) { char tmp2[8] = {0}; - sprintf (tmp2, "%02X", (int) (unsigned char) keyInfo.master_keydata[i + primaryKeyOffset]); - strcat (MasterKeyGUIView, tmp2); + StringCbPrintfA (tmp2, sizeof(tmp2), "%02X", (int) (unsigned char) keyInfo.master_keydata[i + primaryKeyOffset]); + StringCbCatA (MasterKeyGUIView, sizeof(MasterKeyGUIView), tmp2); } HeaderKeyGUIView[0] = 0; for (i = 0; i < NBR_KEY_BYTES_TO_DISPLAY; i++) { char tmp2[8]; - sprintf (tmp2, "%02X", (int) (unsigned char) dk[primaryKeyOffset + i]); - strcat (HeaderKeyGUIView, tmp2); + StringCbPrintfA (tmp2, sizeof(tmp2), "%02X", (int) (unsigned char) dk[primaryKeyOffset + i]); + StringCbCatA (HeaderKeyGUIView, sizeof(HeaderKeyGUIView), tmp2); } if (dots3) diff --git a/src/Format/InPlace.c b/src/Format/InPlace.c index c3d09fb3..228e2e5f 100644 --- a/src/Format/InPlace.c +++ b/src/Format/InPlace.c @@ -34,6 +34,8 @@ IMPORTANT: Due to this issue, functions in this file must not directly interact #include "InPlace.h" +#include + using namespace std; using namespace VeraCrypt; @@ -151,15 +153,15 @@ BOOL CheckRequirementsForNonSysInPlaceEnc (const char *devicePath, BOOL silent) /* Access to the partition */ - strcpy ((char *) devPath, devicePath); - ToUNICODE ((char *) devPath); + StringCbCopyA ((char *) devPath, sizeof(devPath), devicePath); + ToUNICODE ((char *) devPath, sizeof(devPath)); driveLetterNo = GetDiskDeviceDriveLetter (devPath); if (driveLetterNo >= 0) szRootPath[0] = (char) driveLetterNo + 'A'; - if (FakeDosNameForDevice (devicePath, dosDev, devName, FALSE) != 0) + if (FakeDosNameForDevice (devicePath, dosDev, sizeof(dosDev), devName, sizeof(devName),FALSE) != 0) { if (!silent) { @@ -348,13 +350,13 @@ int EncryptPartitionInPlaceBegin (volatile FORMAT_VOL_PARAMETERS *volParams, vol dataAreaSize = GetVolumeDataAreaSize (volParams->hiddenVol, deviceSize); - strcpy ((char *)deviceName, volParams->volumePath); - ToUNICODE ((char *)deviceName); + StringCbCopyA ((char *)deviceName, sizeof(deviceName), volParams->volumePath); + ToUNICODE ((char *)deviceName, sizeof(deviceName)); driveLetter = GetDiskDeviceDriveLetter (deviceName); - if (FakeDosNameForDevice (volParams->volumePath, dosDev, devName, FALSE) != 0) + if (FakeDosNameForDevice (volParams->volumePath, dosDev, sizeof(dosDev),devName, sizeof(devName),FALSE) != 0) { nStatus = ERR_OS_ERROR; goto closing_seq; @@ -710,10 +712,10 @@ int EncryptPartitionInPlaceResume (HANDLE dev, if (dev == INVALID_HANDLE_VALUE) { - strcpy ((char *)deviceName, devicePath); - ToUNICODE ((char *)deviceName); + StringCbCopyA ((char *)deviceName, sizeof(deviceName), devicePath); + ToUNICODE ((char *)deviceName, sizeof(deviceName)); - if (FakeDosNameForDevice (devicePath, dosDev, devName, FALSE) != 0) + if (FakeDosNameForDevice (devicePath, dosDev, sizeof(dosDev),devName, sizeof(devName),FALSE) != 0) { nStatus = ERR_OS_ERROR; goto closing_seq; @@ -1085,9 +1087,9 @@ closing_seq: wchar_t msg[30000] = {0}; wchar_t sizeStr[500] = {0}; - GetSizeString (zeroedSectorCount * sectorSize, sizeStr); + GetSizeString (zeroedSectorCount * sectorSize, sizeStr, sizeof(sizeStr)); - wsprintfW (msg, + StringCbPrintfW (msg, sizeof(msg), GetString ("ZEROED_BAD_SECTOR_COUNT"), zeroedSectorCount, sizeStr); @@ -1369,10 +1371,10 @@ void ShowInPlaceEncErrMsgWAltSteps (char *iniStrId, BOOL bErr) { wchar_t msg[30000]; - wcscpy (msg, GetString (iniStrId)); + StringCbCopyW (msg, sizeof(msg), GetString (iniStrId)); - wcscat (msg, L"\n\n\n"); - wcscat (msg, GetString ("INPLACE_ENC_ALTERNATIVE_STEPS")); + StringCbCatW (msg, sizeof(msg), L"\n\n\n"); + StringCbCatW (msg, sizeof(msg), GetString ("INPLACE_ENC_ALTERNATIVE_STEPS")); if (bErr) ErrorDirect (msg); @@ -1414,7 +1416,7 @@ BOOL SaveNonSysInPlaceEncSettings (int delta, WipeAlgorithmId newWipeAlgorithm) { if (newWipeAlgorithm != TC_WIPE_NONE) { - sprintf (str, "%d", (int) newWipeAlgorithm); + StringCbPrintfA (str, sizeof(str), "%d", (int) newWipeAlgorithm); SaveBufferToFile (str, GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC_WIPE), strlen(str), FALSE); } @@ -1423,7 +1425,7 @@ BOOL SaveNonSysInPlaceEncSettings (int delta, WipeAlgorithmId newWipeAlgorithm) remove (GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC_WIPE)); } - sprintf (str, "%d", count); + StringCbPrintfA (str, sizeof(str), "%d", count); return SaveBufferToFile (str, GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC), strlen(str), FALSE); } diff --git a/src/Format/Tcformat.c b/src/Format/Tcformat.c index 0074a169..d0b247fa 100644 --- a/src/Format/Tcformat.c +++ b/src/Format/Tcformat.c @@ -48,6 +48,8 @@ #include "Wipe.h" #include "Xml.h" +#include + using namespace VeraCrypt; enum wizard_pages @@ -1217,7 +1219,7 @@ void ComboSelChangeEA (HWND hwndDlg) switch (cnt) // Number of ciphers in the cascade { case 2: - swprintf (auxLine, GetString ("TWO_LAYER_CASCADE_HELP"), + StringCbPrintfW (auxLine, sizeof(auxLine), GetString ("TWO_LAYER_CASCADE_HELP"), CipherGetName (cipherIDs[1]), CipherGetKeySize (cipherIDs[1])*8, CipherGetName (cipherIDs[0]), @@ -1225,7 +1227,7 @@ void ComboSelChangeEA (HWND hwndDlg) break; case 3: - swprintf (auxLine, GetString ("THREE_LAYER_CASCADE_HELP"), + StringCbPrintfW (auxLine, sizeof(auxLine), GetString ("THREE_LAYER_CASCADE_HELP"), CipherGetName (cipherIDs[2]), CipherGetKeySize (cipherIDs[2])*8, CipherGetName (cipherIDs[1]), @@ -1235,7 +1237,7 @@ void ComboSelChangeEA (HWND hwndDlg) break; } - wcscpy_s (hyperLink, sizeof(hyperLink) / 2, GetString ("IDC_LINK_MORE_INFO_ABOUT_CIPHER")); + StringCbCopyW (hyperLink, sizeof(hyperLink), GetString ("IDC_LINK_MORE_INFO_ABOUT_CIPHER")); SetWindowTextW (GetDlgItem (hwndDlg, IDC_BOX_HELP), auxLine); } @@ -1454,11 +1456,11 @@ static void UpdateSysEncProgressBar (void) // Status if (locBootEncStatus.TransformWaitingForIdle) - wcscpy (tmpStr, GetString ("PROGRESS_STATUS_WAITING")); + StringCbCopyW (tmpStr, sizeof(tmpStr), GetString ("PROGRESS_STATUS_WAITING")); else - wcscpy (tmpStr, GetString (SystemEncryptionStatus == SYSENC_STATUS_DECRYPTING ? "PROGRESS_STATUS_DECRYPTING" : "PROGRESS_STATUS_ENCRYPTING")); + StringCbCopyW (tmpStr, sizeof(tmpStr), GetString (SystemEncryptionStatus == SYSENC_STATUS_DECRYPTING ? "PROGRESS_STATUS_DECRYPTING" : "PROGRESS_STATUS_ENCRYPTING")); - wcscat (tmpStr, L" "); + StringCbCatW (tmpStr, sizeof(tmpStr), L" "); SetWindowTextW (GetDlgItem (hCurPage, IDC_WRITESPEED), tmpStr); } @@ -1518,17 +1520,17 @@ static void UpdateSysEncControls (void) { wchar_t tmpStr[100]; - wcscpy (tmpStr, GetString ((SysDriveOrPartitionFullyEncrypted (TRUE) || !locBootEncStatus.DriveMounted) ? + StringCbCopyW (tmpStr, sizeof(tmpStr), GetString ((SysDriveOrPartitionFullyEncrypted (TRUE) || !locBootEncStatus.DriveMounted) ? "PROGRESS_STATUS_FINISHED" : "PROGRESS_STATUS_PAUSED")); - wcscat (tmpStr, L" "); + StringCbCatW (tmpStr, sizeof(tmpStr), L" "); // Status SetWindowTextW (GetDlgItem (hCurPage, IDC_WRITESPEED), tmpStr); if (SysDriveOrPartitionFullyEncrypted (TRUE) || SystemEncryptionStatus == SYSENC_STATUS_NONE) { - wcscpy (tmpStr, GetString ("PROCESSED_PORTION_100_PERCENT")); - wcscat (tmpStr, L" "); + StringCbCopyW (tmpStr, sizeof(tmpStr), GetString ("PROCESSED_PORTION_100_PERCENT")); + StringCbCatW (tmpStr, sizeof(tmpStr), L" "); SetWindowTextW (GetDlgItem (hCurPage, IDC_BYTESWRITTEN), tmpStr); } @@ -1699,9 +1701,9 @@ static BOOL GetDevicePathForHiddenOS (void) try { - strncpy (szFileName, BootEncObj->GetPartitionForHiddenOS().DevicePath.c_str(), sizeof(szFileName) - 1); + StringCbCopyA (szFileName, sizeof(szFileName), BootEncObj->GetPartitionForHiddenOS().DevicePath.c_str()); - CreateFullVolumePath (szDiskFile, szFileName, &tmpbDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), szFileName, &tmpbDevice); } catch (Exception &e) { @@ -1804,29 +1806,29 @@ void ShowNonSysInPlaceEncUIStatus (void) switch (NonSysInplaceEncStatus) { case NONSYS_INPLACE_ENC_STATUS_PAUSED: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_PAUSED")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_PAUSED")); break; case NONSYS_INPLACE_ENC_STATUS_PREPARING: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_PREPARING")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_PREPARING")); break; case NONSYS_INPLACE_ENC_STATUS_RESIZING: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_RESIZING")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_RESIZING")); break; case NONSYS_INPLACE_ENC_STATUS_ENCRYPTING: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_ENCRYPTING")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_ENCRYPTING")); break; case NONSYS_INPLACE_ENC_STATUS_FINALIZING: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_FINALIZING")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_FINALIZING")); break; case NONSYS_INPLACE_ENC_STATUS_FINISHED: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_FINISHED")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_FINISHED")); break; case NONSYS_INPLACE_ENC_STATUS_ERROR: - wcscpy (nonSysInplaceEncUIStatus, GetString ("PROGRESS_STATUS_ERROR")); + StringCbCopyW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), GetString ("PROGRESS_STATUS_ERROR")); break; } - wcscat (nonSysInplaceEncUIStatus, L" "); + StringCbCatW (nonSysInplaceEncUIStatus, sizeof(nonSysInplaceEncUIStatus), L" "); SetWindowTextW (GetDlgItem (hCurPage, IDC_WRITESPEED), nonSysInplaceEncUIStatus); } @@ -1974,10 +1976,10 @@ void DisplayRandPool (HWND hPoolDisplay, BOOL bShow) { tmpByte = randPool[row * RANDPOOL_DISPLAY_COLUMNS + col]; - sprintf ((char *) tmp, bRandPoolDispAscii ? ((tmpByte >= 32 && tmpByte < 255 && tmpByte != '&') ? " %c " : " . ") : "%02X ", tmpByte); - strcat ((char *) outRandPoolDispBuffer, (char *) tmp); + StringCbPrintfA ((char *) tmp, sizeof(tmp), bRandPoolDispAscii ? ((tmpByte >= 32 && tmpByte < 255 && tmpByte != '&') ? " %c " : " . ") : "%02X ", tmpByte); + StringCbCatA ((char *) outRandPoolDispBuffer, sizeof(outRandPoolDispBuffer), (char *) tmp); } - strcat ((char *) outRandPoolDispBuffer, "\n"); + StringCbCatA ((char *) outRandPoolDispBuffer, sizeof(outRandPoolDispBuffer), "\n"); } SetWindowText (hPoolDisplay, (char *) outRandPoolDispBuffer); @@ -2300,7 +2302,7 @@ static void __cdecl volTransformThreadFunction (void *hwndDlgArg) if (! ((bHiddenVol && !bHiddenVolHost) && errno != EACCES)) // Only ask ask for permission to overwrite an existing volume if we're not creating a hidden volume { - _snwprintf (szTmp, sizeof szTmp / 2, + StringCbPrintfW (szTmp, sizeof szTmp, GetString (errno == EACCES ? "READONLYPROMPT" : "OVERWRITEPROMPT"), szDiskFile); @@ -2463,7 +2465,7 @@ static void __cdecl volTransformThreadFunction (void *hwndDlgArg) } else if (!(bHiddenVolHost && hiddenVolHostDriveNo < 0)) // If the error was not that the hidden volume host could not be mounted (this error has already been reported to the user) { - swprintf (szMsg, GetString ("CREATE_FAILED"), szDiskFile); + StringCbPrintfW (szMsg, sizeof(szMsg), GetString ("CREATE_FAILED"), szDiskFile); MessageBoxW (hwndDlg, szMsg, lpszTitle, ICON_HAND); } @@ -2904,11 +2906,11 @@ int PrintFreeSpace (HWND hwndTextBox, char *lpszDrive, PLARGE_INTEGER lDiskFree) if (bHiddenVol && !bHiddenVolHost) // If it's a hidden volume { - _snwprintf (szTmp2, sizeof szTmp2 / 2, GetString (nResourceString), ((double) lDiskFree->QuadPart) / nMultiplier); + StringCbPrintfW (szTmp2, sizeof szTmp2, GetString (nResourceString), ((double) lDiskFree->QuadPart) / nMultiplier); SetWindowTextW (GetDlgItem (hwndTextBox, IDC_SIZEBOX), szTmp2); } else - _snwprintf (szTmp2, sizeof szTmp2 / 2, GetString (nResourceString), lpszDrive, ((double) lDiskFree->QuadPart) / nMultiplier); + StringCbPrintfW (szTmp2, sizeof szTmp2, GetString (nResourceString), lpszDrive, ((double) lDiskFree->QuadPart) / nMultiplier); SetWindowTextW (hwndTextBox, szTmp2); @@ -2925,7 +2927,7 @@ void DisplaySizingErrorText (HWND hwndTextBox) if (translateWin32Error (szTmp, sizeof (szTmp) / sizeof(szTmp[0]))) { wchar_t szTmp2[1024]; - wsprintfW (szTmp2, L"%s\n%s", GetString ("CANNOT_CALC_SPACE"), szTmp); + StringCbPrintfW (szTmp2, sizeof(szTmp2), L"%s\n%s", GetString ("CANNOT_CALC_SPACE"), szTmp); SetWindowTextW (hwndTextBox, szTmp2); } else @@ -3152,8 +3154,8 @@ static BOOL FinalPreTransformPrompts (void) int driveNo; WCHAR deviceName[MAX_PATH]; - strcpy ((char *)deviceName, szFileName); - ToUNICODE ((char *)deviceName); + StringCbCopyA ((char *)deviceName, sizeof(deviceName), szFileName); + ToUNICODE ((char *)deviceName, sizeof(deviceName)); driveNo = GetDiskDeviceDriveLetter (deviceName); @@ -3171,7 +3173,7 @@ static BOOL FinalPreTransformPrompts (void) if (!GetDriveLabel (driveNo, volumeLabel, sizeof (volumeLabel))) volumeLabel[0] = 0; - swprintf_s (drive, sizeof (drive)/2, volumeLabel[0] ? L" (%hc: '%s')" : L" (%hc:%s)", 'A' + driveNo, volumeLabel[0] ? volumeLabel : L""); + StringCbPrintfW (drive, sizeof (drive), volumeLabel[0] ? L" (%hc: '%s')" : L" (%hc:%s)", 'A' + driveNo, volumeLabel[0] ? volumeLabel : L""); } else { @@ -3180,9 +3182,9 @@ static BOOL FinalPreTransformPrompts (void) } if (bHiddenOS && bHiddenVolHost) - swprintf (szTmp, GetString ("OVERWRITEPROMPT_DEVICE_HIDDEN_OS_PARTITION"), szFileName, drive); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("OVERWRITEPROMPT_DEVICE_HIDDEN_OS_PARTITION"), szFileName, drive); else - swprintf (szTmp, GetString (bInPlaceEncNonSys ? "NONSYS_INPLACE_ENC_CONFIRM" : "OVERWRITEPROMPT_DEVICE"), type, szFileName, drive); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString (bInPlaceEncNonSys ? "NONSYS_INPLACE_ENC_CONFIRM" : "OVERWRITEPROMPT_DEVICE"), type, szFileName, drive); x = MessageBoxW (MainDlg, szTmp, lpszTitle, YES_NO | MB_ICONWARNING | (bInPlaceEncNonSys ? MB_DEFBUTTON1 : MB_DEFBUTTON2)); @@ -3208,27 +3210,27 @@ static BOOL FinalPreTransformPrompts (void) wchar_t tmpMcOption1 [500]; wchar_t tmpMcOptionCancel [50]; - wcscpy (tmpMcMsg, GetString("OVERWRITEPROMPT_DEVICE_SECOND_WARNING_LOTS_OF_DATA")); - wcscpy (tmpMcOption1, GetString("ERASE_FILES_BY_CREATING_VOLUME")); - wcscpy (tmpMcOptionCancel, GetString("CANCEL")); + StringCbCopyW (tmpMcMsg, sizeof(tmpMcMsg), GetString("OVERWRITEPROMPT_DEVICE_SECOND_WARNING_LOTS_OF_DATA")); + StringCbCopyW (tmpMcOption1, sizeof(tmpMcOption1), GetString("ERASE_FILES_BY_CREATING_VOLUME")); + StringCbCopyW (tmpMcOptionCancel, sizeof(tmpMcOptionCancel), GetString("CANCEL")); - wcscat (tmpMcMsg, L"\n\n"); - wcscat (tmpMcMsg, GetString("DRIVE_LETTER_ITEM")); - swprintf_s (szTmp, sizeof (szTmp)/2, L"%hc:", 'A' + driveNo); - wcscat (tmpMcMsg, szTmp); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), L"\n\n"); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), GetString("DRIVE_LETTER_ITEM")); + StringCbPrintfW (szTmp, sizeof (szTmp), L"%hc:", 'A' + driveNo); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), szTmp); - wcscat (tmpMcMsg, L"\n"); - wcscat (tmpMcMsg, GetString("LABEL_ITEM")); - wcscat (tmpMcMsg, volumeLabel[0] != 0 ? volumeLabel : GetString("NOT_APPLICABLE_OR_NOT_AVAILABLE")); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), L"\n"); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), GetString("LABEL_ITEM")); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), volumeLabel[0] != 0 ? volumeLabel : GetString("NOT_APPLICABLE_OR_NOT_AVAILABLE")); - wcscat (tmpMcMsg, L"\n"); - wcscat (tmpMcMsg, GetString("SIZE_ITEM")); - GetSizeString (nVolumeSize, szTmp); - wcscat (tmpMcMsg, szTmp); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), L"\n"); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), GetString("SIZE_ITEM")); + GetSizeString (nVolumeSize, szTmp, sizeof(szTmp)); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), szTmp); - wcscat (tmpMcMsg, L"\n"); - wcscat (tmpMcMsg, GetString("PATH_ITEM")); - wcscat (tmpMcMsg, deviceName); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), L"\n"); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), GetString("PATH_ITEM")); + StringCbCatW (tmpMcMsg, sizeof(tmpMcMsg), deviceName); wchar_t *tmpStr[] = {L"", tmpMcMsg, tmpMcOption1, tmpMcOptionCancel, 0}; switch (AskMultiChoice ((void **) tmpStr, TRUE)) @@ -3258,8 +3260,8 @@ void HandleOldAssignedDriveLetter (void) WCHAR deviceName[MAX_PATH]; int driveLetter = -1; - strcpy ((char *)deviceName, szDiskFile); - ToUNICODE ((char *)deviceName); + StringCbCopyA ((char *)deviceName, sizeof(deviceName), szDiskFile); + ToUNICODE ((char *)deviceName, sizeof(deviceName)); driveLetter = GetDiskDeviceDriveLetter (deviceName); if (!bHiddenVolHost @@ -3269,7 +3271,7 @@ void HandleOldAssignedDriveLetter (void) char rootPath[] = { (char) driveLetter + 'A', ':', '\\', 0 }; wchar_t szTmp[8192]; - swprintf (szTmp, GetString ("AFTER_FORMAT_DRIVE_LETTER_WARN"), rootPath[0], rootPath[0], rootPath[0], rootPath[0]); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("AFTER_FORMAT_DRIVE_LETTER_WARN"), rootPath[0], rootPath[0], rootPath[0], rootPath[0]); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONWARNING); } } @@ -3302,7 +3304,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa case WM_INITDIALOG: LocalizeDialog (hwndDlg, "IDD_VOL_CREATION_WIZARD_DLG"); - sprintf (PageDebugId, "FORMAT_PAGE_%d", nCurPageNo); + StringCbPrintfA (PageDebugId, sizeof(PageDebugId), "FORMAT_PAGE_%d", nCurPageNo); LastDialogId = PageDebugId; switch (nCurPageNo) @@ -3760,16 +3762,16 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (bHiddenVolHost) { - wcsncpy (str, GetString ("SIZE_HELP_HIDDEN_HOST_VOL"), sizeof (str) / 2); + StringCbCopyW (str, sizeof(str), GetString ("SIZE_HELP_HIDDEN_HOST_VOL")); } else { - wcsncpy (str, GetString (bHiddenVol ? "SIZE_HELP_HIDDEN_VOL" : "SIZE_HELP"), sizeof (str) / 2); + StringCbCopyW (str, sizeof(str), GetString (bHiddenVol ? "SIZE_HELP_HIDDEN_VOL" : "SIZE_HELP")); } if (bDevice && !(bHiddenVol && !bHiddenVolHost)) // If raw device but not a hidden volume { - _snwprintf (str, sizeof str / 2, L"%s%s", + StringCbPrintfW (str, sizeof str, L"%s%s", GetString ((bHiddenOS && bHiddenVol) ? "SIZE_PARTITION_HIDDEN_SYSENC_HELP" : "SIZE_PARTITION_HELP"), (bHiddenVolHost && !bHiddenOS) ? GetString ("SIZE_PARTITION_HIDDEN_VOL_HELP") : L""); } @@ -3823,7 +3825,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (nUIVolumeSize != 0) { char szTmp[32]; - sprintf (szTmp, "%I64u", nUIVolumeSize); + StringCbPrintfA (szTmp, sizeof(szTmp), "%I64u", nUIVolumeSize); SetWindowText (GetDlgItem (hwndDlg, IDC_SIZEBOX), szTmp); } @@ -3882,7 +3884,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa ToBootPwdField (hwndDlg, IDC_PASSWORD); ToBootPwdField (hwndDlg, IDC_VERIFY); - sprintf (OrigKeyboardLayout, "%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); + StringCbPrintfA (OrigKeyboardLayout, sizeof(OrigKeyboardLayout), "%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); if ((DWORD) GetKeyboardLayout (NULL) != 0x00000409 && (DWORD) GetKeyboardLayout (NULL) != 0x04090409) { @@ -3909,17 +3911,17 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (bHiddenVolHost) { - wcsncpy (str, GetString (bHiddenOS ? "PASSWORD_SYSENC_OUTERVOL_HELP" : "PASSWORD_HIDDENVOL_HOST_HELP"), sizeof (str) / 2); + StringCbCopyW (str, sizeof(str), GetString (bHiddenOS ? "PASSWORD_SYSENC_OUTERVOL_HELP" : "PASSWORD_HIDDENVOL_HOST_HELP")); } else if (bHiddenVol) { - _snwprintf (str, sizeof str / 2, L"%s%s", + StringCbPrintfW (str, sizeof str, L"%s%s", GetString (bHiddenOS ? "PASSWORD_HIDDEN_OS_HELP" : "PASSWORD_HIDDENVOL_HELP"), GetString ("PASSWORD_HELP")); } else { - wcsncpy (str, GetString ("PASSWORD_HELP"), sizeof (str) / 2); + StringCbCopyW (str, sizeof(str), GetString ("PASSWORD_HELP")); } SendMessage (GetDlgItem (hwndDlg, IDC_PASSWORD), EM_LIMITTEXT, MAX_PASSWORD, 0); @@ -3966,22 +3968,22 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa Init2RadButtonPageYesNo (nNeedToStoreFilesOver4GB); SetWindowTextW (GetDlgItem (GetParent (hwndDlg), IDC_BOX_TITLE), GetString ("FILESYS_PAGE_TITLE")); - wcscpy (szTmp, GetString ("FILESYS_PAGE_HELP_QUESTION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("FILESYS_PAGE_HELP_QUESTION")); if (bHiddenVolHost) - wcscat (szTmp, L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); else { - wcscat (szTmp, L"\n\n\n"); - wcscat (szTmp, GetString ("NOTE_BEGINNING")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("NOTE_BEGINNING")); } - wcscat (szTmp, GetString ("FILESYS_PAGE_HELP_EXPLANATION")); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("FILESYS_PAGE_HELP_EXPLANATION")); if (bHiddenVolHost) { - wcscat (szTmp, L" "); - wcscat (szTmp, GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL")); + StringCbCatW (szTmp, sizeof(szTmp), L" "); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL")); } SetWindowTextW (GetDlgItem (hwndDlg, IDC_BOX_HELP), szTmp); @@ -4049,7 +4051,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa SetWindowTextW (GetDlgItem (GetParent (hwndDlg), IDC_NEXT), GetString ("NEXT")); SetWindowTextW (GetDlgItem (GetParent (hwndDlg), IDC_PREV), GetString ("PREV")); - _snwprintf (szTmp, sizeof szTmp / 2, + StringCbPrintfW (szTmp, sizeof szTmp, GetString (bDontVerifyRescueDisk ? "RESCUE_DISK_BURN_INFO_NO_CHECK" : "RESCUE_DISK_BURN_INFO"), szRescueDiskISO, IsWindowsIsoBurnerAvailable() ? L"" : GetString ("RESCUE_DISK_BURN_INFO_NONWIN_ISO_BURNER")); @@ -4129,7 +4131,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa try { - wsprintfW (finalMsg, + StringCbPrintfW (finalMsg, sizeof(finalMsg), GetString ("SYS_ENCRYPTION_PRETEST_INFO"), BootEncObj->GetSystemDriveConfiguration().DriveNumber); } @@ -4493,12 +4495,12 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa // -50% reserve for filesystem "peculiarities" maxRecomOuterVolFillSize /= 2; - swprintf (szMaxRecomOuterVolFillSize, L"%I64d %s", maxRecomOuterVolFillSize / BYTES_PER_MB, GetString ("MB")); + StringCbPrintfW (szMaxRecomOuterVolFillSize, sizeof(szMaxRecomOuterVolFillSize), L"%I64d %s", maxRecomOuterVolFillSize / BYTES_PER_MB, GetString ("MB")); - swprintf (msg, GetString ("HIDVOL_HOST_FILLING_HELP_SYSENC"), hiddenVolHostDriveNo + 'A', szMaxRecomOuterVolFillSize); + StringCbPrintfW (msg, sizeof(msg), GetString ("HIDVOL_HOST_FILLING_HELP_SYSENC"), hiddenVolHostDriveNo + 'A', szMaxRecomOuterVolFillSize); } else - swprintf (msg, GetString ("HIDVOL_HOST_FILLING_HELP"), hiddenVolHostDriveNo + 'A'); + StringCbPrintfW (msg, sizeof(msg), GetString ("HIDVOL_HOST_FILLING_HELP"), hiddenVolHostDriveNo + 'A'); SetWindowTextW (GetDlgItem (hwndDlg, IDC_BOX_HELP), msg); SetWindowTextW (GetDlgItem (GetParent (hwndDlg), IDC_BOX_TITLE), GetString ("HIDVOL_HOST_FILLING_TITLE")); @@ -4738,9 +4740,9 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa bWarnOuterVolSuitableFileSys = FALSE; // Do not show this warning anymore (this also prevents potential endless repetition due to some race conditions) - wcscpy (szTmp, GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL_CONFIRM")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("FILESYS_PAGE_HELP_EXPLANATION_HIDVOL_CONFIRM")); if (MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2) == IDNO) { @@ -4834,8 +4836,8 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa SetFocus (GetDlgItem (MainDlg, IDC_NEXT)); - strcpy (szFileName, DeferredNonSysInPlaceEncDevices [selPartitionItemId].Path.c_str()); - CreateFullVolumePath (szDiskFile, szFileName, &tmpbDevice); + StringCbCopyA (szFileName, sizeof(szFileName), DeferredNonSysInPlaceEncDevices [selPartitionItemId].Path.c_str()); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), szFileName, &tmpbDevice); nVolumeSize = GetDeviceSize (szDiskFile); if (nVolumeSize == -1) @@ -5298,7 +5300,7 @@ BOOL CALLBACK PageDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (!BrowseFiles (hwndDlg, "OPEN_TITLE", tmpszRescueDiskISO, FALSE, TRUE, NULL)) return 1; - strcpy (szRescueDiskISO, tmpszRescueDiskISO); + StringCbCopyA (szRescueDiskISO, sizeof(szRescueDiskISO), tmpszRescueDiskISO); SetDlgItemText (hwndDlg, IDC_RESCUE_DISK_ISO_PATH, szRescueDiskISO); EnableWindow (GetDlgItem (MainDlg, IDC_NEXT), (GetWindowTextLength (GetDlgItem (hwndDlg, IDC_RESCUE_DISK_ISO_PATH)) > 1)); @@ -5431,7 +5433,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa } SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, 0, szRescueDiskISO); - strcat (szRescueDiskISO, "\\VeraCrypt Rescue Disk.iso"); + StringCbCatA (szRescueDiskISO, sizeof(szRescueDiskISO), "\\VeraCrypt Rescue Disk.iso"); if (IsOSAtLeast (WIN_VISTA)) { @@ -5485,8 +5487,8 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa for (i = 0; i < sizeof (tmp); i++) { char tmp3[8]; - sprintf (tmp3, "%02X", (int) (unsigned char) tmp[i]); - strcat (tmp2, tmp3); + StringCbPrintfA (tmp3, sizeof(tmp3), "%02X", (int) (unsigned char) tmp[i]); + StringCbCatA (tmp2, sizeof(tmp2), tmp3); } tmp2[32] = 0; @@ -5703,9 +5705,9 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa bKeyboardLayoutChanged = TRUE; wchar_t szTmp [4096]; - wcscpy (szTmp, GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST); } @@ -5718,9 +5720,9 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa bKeybLayoutAltKeyWarningShown = TRUE; wchar_t szTmp [4096]; - wcscpy (szTmp, GetString ("ALT_KEY_CHARS_NOT_FOR_SYS_ENCRYPTION")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("ALT_KEY_CHARS_NOT_FOR_SYS_ENCRYPTION")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TOPMOST); } } @@ -6413,7 +6415,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa GetWindowText (GetDlgItem (hCurPage, IDC_COMBO_BOX), szFileName, sizeof (szFileName)); RelativePath2Absolute (szFileName); - CreateFullVolumePath (szDiskFile, szFileName, &tmpbDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), szFileName, &tmpbDevice); if (tmpbDevice != bDevice) { @@ -6885,7 +6887,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa } else if (DeferredNonSysInPlaceEncDevices.size() == 1) { - CreateFullVolumePath (szDiskFile, DeferredNonSysInPlaceEncDevices.front().Path.c_str(), &tmpbDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), DeferredNonSysInPlaceEncDevices.front().Path.c_str(), &tmpbDevice); nVolumeSize = GetDeviceSize (szDiskFile); if (nVolumeSize == -1) @@ -7042,7 +7044,7 @@ retryCDDriveCheck: { wchar_t szTmp[8000]; - swprintf (szTmp, GetString ("RESCUE_DISK_CHECK_FAILED"), + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("RESCUE_DISK_CHECK_FAILED"), IsWindowsIsoBurnerAvailable () ? L"" : GetString ("RESCUE_DISK_CHECK_FAILED_SENTENCE_APPENDIX")); ErrorDirect (szTmp); @@ -7592,7 +7594,7 @@ ovf_end: BOOL tmpbDevice; GetWindowText (GetDlgItem (hCurPage, IDC_COMBO_BOX), szFileName, sizeof (szFileName)); - CreateFullVolumePath (szDiskFile, szFileName, &tmpbDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), szFileName, &tmpbDevice); if (tmpbDevice == bDevice) { diff --git a/src/Mount/Favorites.cpp b/src/Mount/Favorites.cpp index 06c1aa32..93d9c648 100644 --- a/src/Mount/Favorites.cpp +++ b/src/Mount/Favorites.cpp @@ -56,7 +56,7 @@ namespace VeraCrypt string volumeDevPath = favorite.Path; wchar_t resolvedVolumeDevPath[TC_MAX_PATH]; - if (ResolveSymbolicLink (SingleStringToWide (volumeDevPath).c_str(), resolvedVolumeDevPath)) + if (ResolveSymbolicLink (SingleStringToWide (volumeDevPath).c_str(), resolvedVolumeDevPath, sizeof(resolvedVolumeDevPath))) volumeDevPath = WideToSingleString (resolvedVolumeDevPath); char volumeName[TC_MAX_PATH]; @@ -414,7 +414,7 @@ namespace VeraCrypt if (FavoriteVolumes.empty()) return; - AppendMenu (FavoriteVolumesMenu, MF_SEPARATOR, 0, NULL); + AppendMenu (FavoriteVolumesMenu, MF_SEPARATOR, 0, ""); int i = 0; foreach (const FavoriteVolume &favorite, FavoriteVolumes) diff --git a/src/Mount/Hotkeys.c b/src/Mount/Hotkeys.c index 96f9abcd..c0829602 100644 --- a/src/Mount/Hotkeys.c +++ b/src/Mount/Hotkeys.c @@ -13,6 +13,8 @@ #include "Mount.h" #include "Resource.h" +#include + #define MAX_KEY_COMB_NAME_LEN 260 TCHOTKEY Hotkeys [NBR_HOTKEYS]; @@ -46,56 +48,56 @@ BOOL GetKeyName (UINT vKey, wchar_t *keyName) if (vKey >= 0x30 && vKey <= 0x5a) { // ASCII characters - wsprintfW (keyName, L"%hc", (char) vKey); + StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%hc", (char) vKey); } else if (vKey >= 0xE9 && vKey <= 0xF5) { // OEM-specific - wsprintfW (keyName, L"OEM-%d", vKey); + StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM-%d", vKey); } else if (vKey >= VK_F1 && vKey <= VK_F24) { // F1-F24 - wsprintfW (keyName, L"F%d", vKey - VK_F1 + 1); + StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"F%d", vKey - VK_F1 + 1); } else if (vKey >= VK_NUMPAD0 && vKey <= VK_NUMPAD9) { // Numpad numbers - wsprintfW (keyName, L"%s %d", GetString ("VK_NUMPAD"), vKey - VK_NUMPAD0); + StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s %d", GetString ("VK_NUMPAD"), vKey - VK_NUMPAD0); } else { switch (vKey) { - case VK_MULTIPLY: wsprintfW (keyName, L"%s *", GetString ("VK_NUMPAD")); break; - case VK_ADD: wsprintfW (keyName, L"%s +", GetString ("VK_NUMPAD")); break; - case VK_SEPARATOR: wsprintfW (keyName, L"%s Separator", GetString ("VK_NUMPAD")); break; - case VK_SUBTRACT: wsprintfW (keyName, L"%s -", GetString ("VK_NUMPAD")); break; - case VK_DECIMAL: wsprintfW (keyName, L"%s .", GetString ("VK_NUMPAD")); break; - case VK_DIVIDE: wsprintfW (keyName, L"%s /", GetString ("VK_NUMPAD")); break; - case VK_OEM_1: wcscpy (keyName, L"OEM 1 (';')"); break; - case VK_OEM_PLUS: wcscpy (keyName, L"+"); break; - case VK_OEM_COMMA: wcscpy (keyName, L","); break; - case VK_OEM_MINUS: wcscpy (keyName, L"-"); break; - case VK_OEM_PERIOD: wcscpy (keyName, L"."); break; - case VK_OEM_2: wcscpy (keyName, L"OEM 2 ('/')"); break; - case VK_OEM_3: wcscpy (keyName, L"OEM 3 (`)"); break; - case VK_OEM_4: wcscpy (keyName, L"OEM 4 ('[')"); break; - case VK_OEM_5: wcscpy (keyName, L"OEM 5 ('\\')"); break; - case VK_OEM_6: wcscpy (keyName, L"OEM 6 (']')"); break; - case VK_OEM_7: wcscpy (keyName, L"OEM 7 (')"); break; - case VK_OEM_8: wcscpy (keyName, L"OEM 8"); break; - case VK_OEM_AX: wcscpy (keyName, L"OEM AX"); break; - case VK_OEM_102: wcscpy (keyName, L"OEM 102"); break; - case VK_ICO_HELP: wcscpy (keyName, L"ICO_HELP"); break; - case VK_ICO_00: wcscpy (keyName, L"ICO_00"); break; - case VK_ICO_CLEAR: wcscpy (keyName, L"ICO_CLEAR"); break; - case VK_ATTN: wcscpy (keyName, L"Attn"); break; - case VK_CRSEL: wcscpy (keyName, L"CrSel"); break; - case VK_EXSEL: wcscpy (keyName, L"ExSel"); break; - case VK_EREOF: wcscpy (keyName, L"Erase EOF"); break; - case VK_PA1: wcscpy (keyName, L"PA1"); break; - case VK_OEM_CLEAR: wcscpy (keyName, L"OEM Clear"); break; + case VK_MULTIPLY: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s *", GetString ("VK_NUMPAD")); break; + case VK_ADD: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s +", GetString ("VK_NUMPAD")); break; + case VK_SEPARATOR: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s Separator", GetString ("VK_NUMPAD")); break; + case VK_SUBTRACT: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s -", GetString ("VK_NUMPAD")); break; + case VK_DECIMAL: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s .", GetString ("VK_NUMPAD")); break; + case VK_DIVIDE: StringCbPrintfW (keyName, MAX_KEY_COMB_NAME_LEN, L"%s /", GetString ("VK_NUMPAD")); break; + case VK_OEM_1: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 1 (';')"); break; + case VK_OEM_PLUS: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"+"); break; + case VK_OEM_COMMA: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L","); break; + case VK_OEM_MINUS: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"-"); break; + case VK_OEM_PERIOD: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"."); break; + case VK_OEM_2: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 2 ('/')"); break; + case VK_OEM_3: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 3 (`)"); break; + case VK_OEM_4: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 4 ('[')"); break; + case VK_OEM_5: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 5 ('\\')"); break; + case VK_OEM_6: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 6 (']')"); break; + case VK_OEM_7: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 7 (')"); break; + case VK_OEM_8: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 8"); break; + case VK_OEM_AX: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM AX"); break; + case VK_OEM_102: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM 102"); break; + case VK_ICO_HELP: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"ICO_HELP"); break; + case VK_ICO_00: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"ICO_00"); break; + case VK_ICO_CLEAR: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"ICO_CLEAR"); break; + case VK_ATTN: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"Attn"); break; + case VK_CRSEL: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"CrSel"); break; + case VK_EXSEL: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"ExSel"); break; + case VK_EREOF: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"Erase EOF"); break; + case VK_PA1: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"PA1"); break; + case VK_OEM_CLEAR: StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, L"OEM Clear"); break; case 0: case 1: @@ -107,12 +109,12 @@ BOOL GetKeyName (UINT vKey, wchar_t *keyName) { char key[16]; wchar_t *desc; - sprintf (key, "VKEY_%02X", vKey); + StringCbPrintfA (key, sizeof(key),"VKEY_%02X", vKey); desc = GetString (key); if (desc == UnknownString) result = FALSE; else - wcsncpy (keyName, desc, MAX_KEY_COMB_NAME_LEN); + StringCbCopyW (keyName, MAX_KEY_COMB_NAME_LEN, desc); } } } @@ -226,36 +228,36 @@ static void DisplayHotkeyList (HWND hwndDlg) SendMessageW (hList,LVM_INSERTITEMW,0,(LPARAM)&item); item.iSubItem = 1; - wcscpy (Shortcut, L""); - wcscpy (ShortcutMod, L""); + Shortcut[0] = 0; + ShortcutMod[0] = 0; if (GetKeyName (tmpHotkeys[i].vKeyCode, Shortcut)) { if (tmpHotkeys[i].vKeyModifiers & MOD_CONTROL) { - wcscat (ShortcutMod, GetString ("VK_CONTROL")); - wcscat (ShortcutMod, L"+"); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),GetString ("VK_CONTROL")); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),L"+"); } if (tmpHotkeys[i].vKeyModifiers & MOD_SHIFT) { - wcscat (ShortcutMod, GetString ("VK_SHIFT")); - wcscat (ShortcutMod, L"+"); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),GetString ("VK_SHIFT")); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),L"+"); } if (tmpHotkeys[i].vKeyModifiers & MOD_ALT) { - wcscat (ShortcutMod, GetString ("VK_ALT")); - wcscat (ShortcutMod, L"+"); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),GetString ("VK_ALT")); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),L"+"); } if (tmpHotkeys[i].vKeyModifiers & MOD_WIN) { - wcscat (ShortcutMod, GetString ("VK_WIN")); - wcscat (ShortcutMod, L"+"); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),GetString ("VK_WIN")); + StringCbCatW (ShortcutMod, sizeof(ShortcutMod),L"+"); } - wsprintfW (ShortcutFinal, L"%s%s", ShortcutMod, Shortcut); + StringCbPrintfW (ShortcutFinal, sizeof(ShortcutFinal), L"%s%s", ShortcutMod, Shortcut); item.pszText = ShortcutFinal; } else diff --git a/src/Mount/Mount.c b/src/Mount/Mount.c index 5f6e472e..c63d31eb 100644 --- a/src/Mount/Mount.c +++ b/src/Mount/Mount.c @@ -46,6 +46,8 @@ #include "../Platform/Finally.h" #include "../Platform/ForEach.h" +#include + using namespace VeraCrypt; enum timer_ids @@ -534,7 +536,7 @@ void SaveSettings (HWND hwndDlg) // Drive Letter lLetter = GetSelectedLong (GetDlgItem (hwndDlg, IDC_DRIVELIST)); if (LOWORD (lLetter) != 0xffff) - sprintf (szTmp, "%c:", (char) HIWORD (lLetter)); + StringCbPrintfA (szTmp, sizeof(szTmp), "%c:", (char) HIWORD (lLetter)); ConfigWriteString ("LastSelectedDrive", szTmp); ConfigWriteInt ("CloseSecurityTokenSessionsAfterMount", CloseSecurityTokenSessionsAfterMount); @@ -721,19 +723,19 @@ static void PopulateSysEncContextMenu (HMENU popup, BOOL bToolsOnly) AppendMenuW (popup, MF_STRING, IDM_PERMANENTLY_DECRYPT_SYS, GetString ("PERMANENTLY_DECRYPT")); AppendMenuW (popup, MF_STRING, IDM_ENCRYPT_SYSTEM_DEVICE, GetString ("ENCRYPT")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); } } AppendMenuW (popup, MF_STRING, IDM_CHANGE_SYS_PASSWORD, GetString ("IDM_CHANGE_SYS_PASSWORD")); AppendMenuW (popup, MF_STRING, IDM_CHANGE_SYS_HEADER_KEY_DERIV_ALGO, GetString ("IDM_CHANGE_SYS_HEADER_KEY_DERIV_ALGO")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_SYS_ENC_SETTINGS, GetString ("IDM_SYS_ENC_SETTINGS")); if (!IsHiddenOSRunning()) { - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_CREATE_RESCUE_DISK, GetString ("IDM_CREATE_RESCUE_DISK")); AppendMenuW (popup, MF_STRING, IDM_VERIFY_RESCUE_DISK, GetString ("IDM_VERIFY_RESCUE_DISK")); } @@ -742,10 +744,10 @@ static void PopulateSysEncContextMenu (HMENU popup, BOOL bToolsOnly) { if (SysDriveOrPartitionFullyEncrypted (FALSE) && !IsHiddenOSRunning()) { - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_PERMANENTLY_DECRYPT_SYS, GetString ("PERMANENTLY_DECRYPT")); } - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_VOLUME_PROPERTIES, GetString ("IDPM_PROPERTIES")); } } @@ -764,7 +766,7 @@ BOOL CheckSysEncMountWithoutPBA (const char *devicePath, BOOL quiet) if (strlen (devicePath) < 2) { GetWindowText (GetDlgItem (MainDlg, IDC_VOLUME), szDevicePath, sizeof (szDevicePath)); - CreateFullVolumePath (szDiskFile, szDevicePath, &tmpbDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), szDevicePath, &tmpbDevice); if (!tmpbDevice) { @@ -783,7 +785,7 @@ BOOL CheckSysEncMountWithoutPBA (const char *devicePath, BOOL quiet) } } else - strncpy (szDevicePath, devicePath, sizeof (szDevicePath) - 1); + StringCbCopyA (szDevicePath, sizeof(szDevicePath), devicePath); char *partionPortion = strrchr (szDevicePath, '\\'); @@ -815,7 +817,7 @@ BOOL CheckSysEncMountWithoutPBA (const char *devicePath, BOOL quiet) return FALSE; } - _snprintf (parentDrivePath, + StringCbPrintfA (parentDrivePath, sizeof (parentDrivePath), "\\Device\\Harddisk%d\\Partition0", driveNo); @@ -872,7 +874,7 @@ BOOL TCBootLoaderOnInactiveSysEncDrive (void) if (sscanf (szDevicePath, "\\Device\\Harddisk%d\\Partition", &driveNo) != 1) return FALSE; - _snprintf (parentDrivePath, + StringCbPrintfA (parentDrivePath, sizeof (parentDrivePath), "\\Device\\Harddisk%d\\Partition0", driveNo); @@ -949,15 +951,16 @@ static void LaunchVolCreationWizard (HWND hwndDlg, const char *arg) PROCESS_INFORMATION pi; ZeroMemory (&si, sizeof (si)); - strcpy (++tmp, "VeraCrypt Format.exe\""); + *tmp = 0; + StringCbCopyA (t, sizeof(t), "\\VeraCrypt Format.exe\""); if (!FileExists(t)) Error ("VOL_CREATION_WIZARD_NOT_FOUND"); // Display a user-friendly error message and advise what to do if (strlen (arg) > 0) { - strcat (t, " "); - strcat (t, arg); + StringCbCatA (t, sizeof(t), " "); + StringCbCatA (t, sizeof(t), arg); } if (!CreateProcess (NULL, (LPSTR) t, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) @@ -1068,7 +1071,8 @@ void LoadDriveLetters (HWND hTree, int drive) listItem.iItem = item++; listItem.pszText = szTmp; - strcpy (szTmp, " "); + szTmp[0] = ' '; + szTmp[1] = 0; listItem.lParam = MAKELONG (TC_MLIST_ITEM_SYS_DRIVE, ENC_SYSDRIVE_PSEUDO_DRIVE_LETTER); @@ -1082,7 +1086,7 @@ void LoadDriveLetters (HWND hTree, int drive) // Fully encrypted if (SysDriveOrPartitionFullyEncrypted (TRUE)) { - wcscpy (szTmpW, GetString ("SYSTEM_DRIVE")); + StringCbCopyW (szTmpW, sizeof(szTmpW), GetString ("SYSTEM_DRIVE")); } else { @@ -1094,23 +1098,23 @@ void LoadDriveLetters (HWND hTree, int drive) if (BootEncStatus.SetupMode != SetupDecryption) { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_DRIVE_ENCRYPTING"), (double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0); } else { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_DRIVE_DECRYPTING"), 100.0 - ((double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0)); } } else { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_DRIVE_PARTIALLY_ENCRYPTED"), (double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0); } @@ -1118,7 +1122,7 @@ void LoadDriveLetters (HWND hTree, int drive) ListSubItemSetW (hTree, listItem.iItem, 1, szTmpW); - GetSizeString (GetSysEncDeviceSize(TRUE), szTmpW); + GetSizeString (GetSysEncDeviceSize(TRUE), szTmpW, sizeof(szTmpW)); ListSubItemSetW (hTree, listItem.iItem, 2, szTmpW); EAGetName (szTmp, propSysEnc.ea); @@ -1182,7 +1186,7 @@ void LoadDriveLetters (HWND hTree, int drive) // Fully encrypted if (SysDriveOrPartitionFullyEncrypted (TRUE)) { - wcscpy (szTmpW, GetString (IsHiddenOSRunning() ? "HIDDEN_SYSTEM_PARTITION" : "SYSTEM_PARTITION")); + StringCbCopyW (szTmpW, sizeof(szTmpW), GetString (IsHiddenOSRunning() ? "HIDDEN_SYSTEM_PARTITION" : "SYSTEM_PARTITION")); } else { @@ -1194,23 +1198,23 @@ void LoadDriveLetters (HWND hTree, int drive) if (BootEncStatus.SetupMode != SetupDecryption) { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_PARTITION_ENCRYPTING"), (double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0); } else { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_PARTITION_DECRYPTING"), 100.0 - ((double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0)); } } else { - _snwprintf (szTmpW, - sizeof szTmpW/2, + StringCbPrintfW (szTmpW, + sizeof szTmpW, GetString ("SYSTEM_PARTITION_PARTIALLY_ENCRYPTED"), (double) GetSysEncDeviceEncryptedPartSize (TRUE) / (double) GetSysEncDeviceSize (TRUE) * 100.0); } @@ -1220,7 +1224,7 @@ void LoadDriveLetters (HWND hTree, int drive) } else { - ToSBCS (driver.wszVolume[i]); + ToSBCS (driver.wszVolume[i], sizeof(driver.wszVolume[i])); char *path = (char *) driver.wszVolume[i]; if (memcmp (path, "\\??\\", 4) == 0) @@ -1235,7 +1239,7 @@ void LoadDriveLetters (HWND hTree, int drive) ListSubItemSet (hTree, listItem.iItem, 1, (char *) FitPathInGfxWidth (hTree, hUserFont, ListView_GetColumnWidth (hTree, 1) - GetTextGfxWidth (hTree, L"___", hUserFont), path).c_str()); } - GetSizeString (bSysEncPartition ? GetSysEncDeviceSize(TRUE) : driver.diskLength[i], szTmpW); + GetSizeString (bSysEncPartition ? GetSysEncDeviceSize(TRUE) : driver.diskLength[i], szTmpW, sizeof(szTmpW)); ListSubItemSetW (hTree, listItem.iItem, 2, szTmpW); EAGetName (szTmp, bSysEncPartition ? propSysEnc.ea : driver.ea[i]); @@ -1276,7 +1280,7 @@ void LoadDriveLetters (HWND hTree, int drive) wchar_t szTmp[4096]; VolumeNotificationsList.bHidVolDamagePrevReported[i] = TRUE; - swprintf (szTmp, GetString ("DAMAGE_TO_HIDDEN_VOLUME_PREVENTED"), i+'A'); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("DAMAGE_TO_HIDDEN_VOLUME_PREVENTED"), i+'A'); SetForegroundWindow (GetParent(hTree)); MessageBoxW (GetParent(hTree), szTmp, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST); } @@ -1554,9 +1558,9 @@ BOOL CALLBACK PasswordChangeDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPAR bKeyboardLayoutChanged = TRUE; wchar_t szTmp [4096]; - wcscpy (szTmp, GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST); } @@ -1570,9 +1574,9 @@ BOOL CALLBACK PasswordChangeDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPAR bKeybLayoutAltKeyWarningShown = TRUE; wchar_t szTmp [4096]; - wcscpy (szTmp, GetString ("ALT_KEY_CHARS_NOT_FOR_SYS_ENCRYPTION")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("ALT_KEY_CHARS_NOT_FOR_SYS_ENCRYPTION")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TOPMOST); } } @@ -1899,12 +1903,12 @@ BOOL CALLBACK PasswordDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa wstring label = GetFavoriteVolumeLabel (PasswordDlgVolume); if (!label.empty()) { - wsprintfW (s, GetString ("ENTER_PASSWORD_FOR_LABEL"), label.c_str()); + StringCbPrintfW (s, sizeof(s), GetString ("ENTER_PASSWORD_FOR_LABEL"), label.c_str()); } else { - wsprintfW (s, GetString ("ENTER_PASSWORD_FOR"), "___"); - wsprintfW (s, GetString ("ENTER_PASSWORD_FOR"), FitPathInGfxWidth (hwndDlg, WindowTitleBarFont, rect.right - rect.left - GetTextGfxWidth (hwndDlg, s, WindowTitleBarFont), PasswordDlgVolume).c_str()); + StringCbPrintfW (s, sizeof(s), GetString ("ENTER_PASSWORD_FOR"), "___"); + StringCbPrintfW (s, sizeof(s), GetString ("ENTER_PASSWORD_FOR"), FitPathInGfxWidth (hwndDlg, WindowTitleBarFont, rect.right - rect.left - GetTextGfxWidth (hwndDlg, s, WindowTitleBarFont), PasswordDlgVolume).c_str()); } SetWindowTextW (hwndDlg, s); @@ -1957,7 +1961,7 @@ BOOL CALLBACK PasswordDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa SetWindowText (GetDlgItem (hwndDlg, IDC_PASSWORD), tmp); SetWindowText (GetDlgItem (hwndDlg, IDC_PASSWORD), ""); - sprintf (OrigKeyboardLayout, "%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); + StringCbPrintfA (OrigKeyboardLayout, sizeof(OrigKeyboardLayout),"%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); DWORD keybLayout = (DWORD) LoadKeyboardLayout ("00000409", KLF_ACTIVATE); @@ -2015,9 +2019,9 @@ BOOL CALLBACK PasswordDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa } wchar_t szTmp [4096]; - wcscpy (szTmp, GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); - wcscat (szTmp, L"\n\n"); - wcscat (szTmp, GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); + StringCbCopyW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_CHANGE_PREVENTED")); + StringCbCatW (szTmp, sizeof(szTmp), L"\n\n"); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("KEYB_LAYOUT_SYS_ENC_EXPLANATION")); MessageBoxW (MainDlg, szTmp, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST); } } @@ -2152,9 +2156,12 @@ BOOL CALLBACK PasswordDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa while (count-- > 0) { KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); - DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); - FirstKeyFile = KeyFileAdd (FirstKeyFile, kf); - KeyFilesEnable = TRUE; + if (kf) + { + DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); + FirstKeyFile = KeyFileAdd (FirstKeyFile, kf); + KeyFilesEnable = TRUE; + } } SetCheckBox (hwndDlg, IDC_KEYFILES_ENABLE, KeyFilesEnable); @@ -2351,23 +2358,27 @@ BOOL CALLBACK PreferencesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM if (lw == IDC_MORE_SETTINGS) { HMENU popup = CreatePopupMenu (); + if (popup) + { + AppendMenuW (popup, MF_STRING, IDM_LANGUAGE, GetString ("IDM_LANGUAGE")); + AppendMenuW (popup, MF_STRING, IDM_HOTKEY_SETTINGS, GetString ("IDM_HOTKEY_SETTINGS")); + AppendMenuW (popup, MF_STRING, IDM_PERFORMANCE_SETTINGS, GetString ("IDM_PERFORMANCE_SETTINGS")); + AppendMenuW (popup, MF_STRING, IDM_SYSENC_SETTINGS, GetString ("IDM_SYSENC_SETTINGS")); + AppendMenuW (popup, MF_STRING, IDM_SYS_FAVORITES_SETTINGS, GetString ("IDM_SYS_FAVORITES_SETTINGS")); + AppendMenuW (popup, MF_STRING, IDM_DEFAULT_KEYFILES, GetString ("IDM_DEFAULT_KEYFILES")); + AppendMenuW (popup, MF_STRING, IDM_TOKEN_PREFERENCES, GetString ("IDM_TOKEN_PREFERENCES")); - AppendMenuW (popup, MF_STRING, IDM_LANGUAGE, GetString ("IDM_LANGUAGE")); - AppendMenuW (popup, MF_STRING, IDM_HOTKEY_SETTINGS, GetString ("IDM_HOTKEY_SETTINGS")); - AppendMenuW (popup, MF_STRING, IDM_PERFORMANCE_SETTINGS, GetString ("IDM_PERFORMANCE_SETTINGS")); - AppendMenuW (popup, MF_STRING, IDM_SYSENC_SETTINGS, GetString ("IDM_SYSENC_SETTINGS")); - AppendMenuW (popup, MF_STRING, IDM_SYS_FAVORITES_SETTINGS, GetString ("IDM_SYS_FAVORITES_SETTINGS")); - AppendMenuW (popup, MF_STRING, IDM_DEFAULT_KEYFILES, GetString ("IDM_DEFAULT_KEYFILES")); - AppendMenuW (popup, MF_STRING, IDM_TOKEN_PREFERENCES, GetString ("IDM_TOKEN_PREFERENCES")); - - RECT rect; - GetWindowRect (GetDlgItem (hwndDlg, IDC_MORE_SETTINGS), &rect); + RECT rect; + GetWindowRect (GetDlgItem (hwndDlg, IDC_MORE_SETTINGS), &rect); - int menuItem = TrackPopupMenu (popup, TPM_RETURNCMD | TPM_LEFTBUTTON, rect.left + 2, rect.top + 2, 0, hwndDlg, NULL); - DestroyMenu (popup); + int menuItem = TrackPopupMenu (popup, TPM_RETURNCMD | TPM_LEFTBUTTON, rect.left + 2, rect.top + 2, 0, hwndDlg, NULL); + DestroyMenu (popup); - SendMessage (MainDlg, WM_COMMAND, menuItem, NULL); - return 1; + SendMessage (MainDlg, WM_COMMAND, menuItem, NULL); + return 1; + } + else + return 0; } if (HIWORD (wParam) == BN_CLICKED) @@ -2739,7 +2750,7 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP // Size ListItemAddW (list, i, GetString ("SIZE")); - swprintf (sw, L"%I64u %s", prop.diskLength, GetString ("BYTES")); + StringCbPrintfW (sw, sizeof(sw), L"%I64u %s", prop.diskLength, GetString ("BYTES")); ListSubItemSetW (list, i++, 1, sw); // Type @@ -2802,7 +2813,7 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP // Primary key ListItemAddW (list, i, GetString ("KEY_SIZE")); - wsprintfW (sw, L"%d %s", size * 8, GetString ("BITS")); + StringCbPrintfW (sw, sizeof(sw), L"%d %s", size * 8, GetString ("BITS")); ListSubItemSetW (list, i++, 1, sw); if (strcmp (EAGetModeName (prop.ea, prop.mode, TRUE), "XTS") == 0) @@ -2817,7 +2828,7 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP // Tweak key (LRW) ListItemAddW (list, i, GetString ("SECONDARY_KEY_SIZE_LRW")); - swprintf (sw, L"%d %s", CipherGetBlockSize (EAGetFirstCipher(prop.ea))*8, GetString ("BITS")); + StringCbPrintfW (sw, sizeof(sw), L"%d %s", CipherGetBlockSize (EAGetFirstCipher(prop.ea))*8, GetString ("BITS")); ListSubItemSetW (list, i++, 1, sw); } } @@ -2830,20 +2841,20 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP wchar_t tmpstr[64]; int i = EAGetLastCipher(prop.ea); - swprintf (sw, L"%d", CipherGetBlockSize(i)*8); + StringCbPrintfW (sw, sizeof(sw), L"%d", CipherGetBlockSize(i)*8); while (i = EAGetPreviousCipher(prop.ea, i)) { - swprintf (tmpstr, L"/%d", CipherGetBlockSize(i)*8); - wcscat (sw, tmpstr); + StringCbPrintfW (tmpstr, sizeof(tmpstr), L"/%d", CipherGetBlockSize(i)*8); + StringCbCatW (sw, sizeof(sw), tmpstr); } - wcscat (sw, L" "); + StringCbCatW (sw, sizeof(sw), L" "); } else { - swprintf (sw, L"%d ", CipherGetBlockSize (EAGetFirstCipher(prop.ea))*8); + StringCbPrintfW (sw, sizeof(sw), L"%d ", CipherGetBlockSize (EAGetFirstCipher(prop.ea))*8); } - wcscat (sw, GetString ("BITS")); + StringCbCatW (sw, sizeof(sw), GetString ("BITS")); ListSubItemSetW (list, i++, 1, sw); // Mode @@ -2906,7 +2917,7 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP { // Volume format version ListItemAddW (list, i, GetString ("VOLUME_FORMAT_VERSION")); - sprintf (szTmp, "%d", prop.volFormatVersion); + StringCbPrintfA (szTmp, sizeof(szTmp), "%d", prop.volFormatVersion); ListSubItemSet (list, i++, 1, szTmp); // Backup header @@ -2916,12 +2927,12 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP // Total data read ListItemAddW (list, i, GetString ("TOTAL_DATA_READ")); - GetSizeString (prop.totalBytesRead, sw); + GetSizeString (prop.totalBytesRead, sw, sizeof(sw)); ListSubItemSetW (list, i++, 1, sw); // Total data written ListItemAddW (list, i, GetString ("TOTAL_DATA_WRITTEN")); - GetSizeString (prop.totalBytesWritten, sw); + GetSizeString (prop.totalBytesWritten, sw, sizeof(sw)); ListSubItemSetW (list, i++, 1, sw); if (bSysEnc) @@ -2939,8 +2950,8 @@ BOOL CALLBACK VolumePropertiesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP else { - _snwprintf (sw, - sizeof sw/2, + StringCbPrintfW (sw, + sizeof sw, GetString ("PROCESSED_PORTION_X_PERCENT"), (double) GetSysEncDeviceEncryptedPartSize (FALSE) / (double) GetSysEncDeviceSize (FALSE) * 100.0); @@ -3078,6 +3089,7 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa char sysDir[MAX_PATH]; char volName[MAX_PATH]; int drive; + char* ptr; GetDlgItemText (hwndDlg, IDC_DIRECTORY, dstDir, sizeof dstDir); volName[0] = 0; @@ -3111,21 +3123,22 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa if (volName[1] != 0) { volName[0] = '"'; - strcat (volName, "\""); + StringCbCatA (volName, sizeof(volName), "\""); } GetModuleFileName (NULL, appDir, sizeof (appDir)); - strrchr (appDir, '\\')[0] = 0; + if (ptr = strrchr (appDir, '\\')) + ptr[0] = 0; WaitCursor (); GetSystemDirectory (sysDir, sizeof (sysDir)); - sprintf (dstPath, "%s\\VeraCrypt", dstDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt", dstDir); CreateDirectory (dstPath, NULL); // Main app - sprintf (srcPath, "%s\\VeraCrypt.exe", appDir); - sprintf (dstPath, "%s\\VeraCrypt\\VeraCrypt.exe", dstDir); + StringCbPrintfA (srcPath, sizeof(srcPath), "%s\\VeraCrypt.exe", appDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt\\VeraCrypt.exe", dstDir); if (!TCCopyFile (srcPath, dstPath)) { handleWin32Error (hwndDlg); @@ -3135,8 +3148,8 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa // Wizard if (copyWizard) { - sprintf (srcPath, "%s\\VeraCrypt Format.exe", appDir); - sprintf (dstPath, "%s\\VeraCrypt\\VeraCrypt Format.exe", dstDir); + StringCbPrintfA (srcPath, sizeof(srcPath), "%s\\VeraCrypt Format.exe", appDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt\\VeraCrypt Format.exe", dstDir); if (!TCCopyFile (srcPath, dstPath)) { handleWin32Error (hwndDlg); @@ -3145,8 +3158,8 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa } // Driver - sprintf (srcPath, "%s\\veracrypt.sys", appDir); - sprintf (dstPath, "%s\\VeraCrypt\\veracrypt.sys", dstDir); + StringCbPrintfA (srcPath, sizeof(srcPath), "%s\\veracrypt.sys", appDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt\\veracrypt.sys", dstDir); if (!TCCopyFile (srcPath, dstPath)) { handleWin32Error (hwndDlg); @@ -3154,8 +3167,8 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa } // Driver x64 - sprintf (srcPath, "%s\\veracrypt-x64.sys", appDir); - sprintf (dstPath, "%s\\VeraCrypt\\veracrypt-x64.sys", dstDir); + StringCbPrintfA (srcPath, sizeof(srcPath), "%s\\veracrypt-x64.sys", appDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt\\veracrypt-x64.sys", dstDir); if (!TCCopyFile (srcPath, dstPath)) { handleWin32Error (hwndDlg); @@ -3165,13 +3178,13 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa if (GetPreferredLangId () && strcmp (GetPreferredLangId (), "en") != 0) { // Language pack - sprintf (srcPath, "%s\\Language.%s.xml", appDir, GetPreferredLangId ()); - sprintf (dstPath, "%s\\VeraCrypt\\Language.%s.xml", dstDir, GetPreferredLangId ()); + StringCbPrintfA (srcPath, sizeof(srcPath), "%s\\Language.%s.xml", appDir, GetPreferredLangId ()); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\VeraCrypt\\Language.%s.xml", dstDir, GetPreferredLangId ()); TCCopyFile (srcPath, dstPath); } // AutoRun - sprintf (dstPath, "%s\\autorun.inf", dstDir); + StringCbPrintfA (dstPath, sizeof(dstPath), "%s\\autorun.inf", dstDir); DeleteFile (dstPath); if (bAutoRun) { @@ -3187,7 +3200,7 @@ BOOL CALLBACK TravelerDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa goto stop; } - sprintf (autoMount, "VeraCrypt\\VeraCrypt.exe /q background%s%s%s%s /m rm /v %s", + StringCbPrintfA (autoMount, sizeof(autoMount), "VeraCrypt\\VeraCrypt.exe /q background%s%s%s%s /m rm /v %s", drive > 0 ? driveLetter : "", bExplore ? " /e" : "", bCacheInDriver ? " /c y" : "", @@ -3452,7 +3465,7 @@ static BOOL Mount (HWND hwndDlg, int nDosDriveNo, char *szFileName) } else if (!Silent) { - strcpy (PasswordDlgVolume, szFileName); + StringCbCopyA (PasswordDlgVolume, sizeof(PasswordDlgVolume), szFileName); if (!AskVolumePassword (hwndDlg, &VolumePassword, NULL, TRUE)) goto ret; @@ -3615,7 +3628,7 @@ retry: wchar_t msg[4096]; VolumeNotificationsList.bHidVolDamagePrevReported [unmount.nDosDriveNo] = TRUE; - swprintf (msg, GetString ("DAMAGE_TO_HIDDEN_VOLUME_PREVENTED"), unmount.nDosDriveNo + 'A'); + StringCbPrintfW (msg, sizeof(msg), GetString ("DAMAGE_TO_HIDDEN_VOLUME_PREVENTED"), unmount.nDosDriveNo + 'A'); SetForegroundWindow (hwndDlg); MessageBoxW (hwndDlg, msg, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST); @@ -3836,9 +3849,9 @@ static BOOL MountAllDevices (HWND hwndDlg, BOOL bPasswordPrompt) { WCHAR szTmp[4096]; - swprintf (szTmp, GetString (KeyFilesEnable || FirstCmdKeyFile ? "PASSWORD_OR_KEYFILE_WRONG_AUTOMOUNT" : "PASSWORD_WRONG_AUTOMOUNT")); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString (KeyFilesEnable || FirstCmdKeyFile ? "PASSWORD_OR_KEYFILE_WRONG_AUTOMOUNT" : "PASSWORD_WRONG_AUTOMOUNT")); if (CheckCapsLock (hwndDlg, TRUE)) - wcscat (szTmp, GetString ("PASSWORD_WRONG_CAPSLOCK_ON")); + StringCbCatW (szTmp, sizeof(szTmp), GetString ("PASSWORD_WRONG_CAPSLOCK_ON")); MessageBoxW (hwndDlg, szTmp, lpszTitle, MB_ICONWARNING); } @@ -3986,7 +3999,7 @@ static void ChangeSysEncPassword (HWND hwndDlg, BOOL bOnlyChangeKDF) if (CreateSysEncMutex ()) // If no instance of the wizard is currently taking care of system encryption { - sprintf (OrigKeyboardLayout, "%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); + StringCbPrintfA (OrigKeyboardLayout, sizeof(OrigKeyboardLayout), "%08X", (DWORD) GetKeyboardLayout (NULL) & 0xFFFF); bSysEncPwdChangeDlgMode = TRUE; @@ -4251,7 +4264,7 @@ void CreateRescueDisk (void) WaitCursor(); BootEncObj->CreateRescueIsoImage (false, szRescueDiskISO); - _snwprintf (szTmp, sizeof szTmp / 2, + StringCbPrintfW (szTmp, sizeof szTmp, GetString (IsWindowsIsoBurnerAvailable() ? "RESCUE_DISK_NON_WIZARD_CREATION_WIN_ISOBURN" : "RESCUE_DISK_NON_WIZARD_CREATION_BURN"), szRescueDiskISO); @@ -4785,7 +4798,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa { VolumePassword.Length = 0; - strcpy (PasswordDlgVolume, szFileName); + StringCbCopyA (PasswordDlgVolume, sizeof(PasswordDlgVolume),szFileName); if (!AskVolumePassword (hwndDlg, &VolumePassword, NULL, TRUE)) break; @@ -5323,7 +5336,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (MainWindowHidden) { AppendMenuW (popup, MF_STRING, IDM_SHOW_HIDE, GetString ("SHOW_TC")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); } else if (bEnableBkgTask && (!(LastKnownMountList.ulMountedDrives == 0 @@ -5332,12 +5345,12 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa && GetDriverRefCount () < 2))) { AppendMenuW (popup, MF_STRING, IDM_SHOW_HIDE, GetString ("HIDE_TC")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); } AppendMenuW (popup, MF_STRING, IDM_MOUNTALL, GetString ("IDC_MOUNTALL")); AppendMenuW (popup, MF_STRING, IDM_MOUNT_FAVORITE_VOLUMES, GetString ("IDM_MOUNT_FAVORITE_VOLUMES")); AppendMenuW (popup, MF_STRING, IDM_UNMOUNTALL, GetString ("IDC_UNMOUNTALL")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); for (n = 0; n < 2; n++) { @@ -5352,7 +5365,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa wstring label = GetFavoriteVolumeLabel (WideToSingleString (vol)); - wsprintfW (s, L"%s %c: (%s)", + StringCbPrintfW (s, sizeof(s), L"%s %c: (%s)", GetString (n==0 ? "OPEN" : "DISMOUNT"), i + L'A', label.empty() ? vol : label.c_str()); @@ -5360,14 +5373,14 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa } } if (LastKnownMountList.ulMountedDrives != 0) - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); } AppendMenuW (popup, MF_STRING, IDM_HELP, GetString ("MENU_HELP")); AppendMenuW (popup, MF_STRING, IDM_HOMEPAGE_SYSTRAY, GetString ("HOMEPAGE")); AppendMenuW (popup, MF_STRING, IDM_PREFERENCES, GetString ("IDM_PREFERENCES")); AppendMenuW (popup, MF_STRING, IDM_ABOUT, GetString ("IDM_ABOUT")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDCANCEL, GetString ("EXIT")); GetCursorPos (&pos); @@ -5393,7 +5406,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (Dismount (hwndDlg, sel - TRAYICON_MENU_DRIVE_OFFSET - 26)) { wchar_t txt [2048]; - wsprintfW (txt, GetString ("VOLUME_MOUNTED_AS_DRIVE_LETTER_X_DISMOUNTED"), sel - TRAYICON_MENU_DRIVE_OFFSET - 26 + L'A'); + StringCbPrintfW (txt, sizeof(txt), GetString ("VOLUME_MOUNTED_AS_DRIVE_LETTER_X_DISMOUNTED"), sel - TRAYICON_MENU_DRIVE_OFFSET - 26 + L'A'); InfoBalloonDirect (GetString ("SUCCESSFULLY_DISMOUNTED"), txt); } @@ -5499,7 +5512,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (wcsstr (vol, L"\\??\\") == vol) vol += 4; - _snprintf (volp, sizeof(volp), "%ls", vol); + StringCbPrintfA (volp, sizeof(volp), "%ls", vol); if (IsVolumeDeviceHosted (volp)) { @@ -5603,7 +5616,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa // No mounted volume at this drive letter AppendMenuW (popup, MF_STRING, IDM_MOUNT_VOLUME, GetString ("IDM_MOUNT_VOLUME")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDPM_SELECT_FILE_AND_MOUNT, GetString ("SELECT_FILE_AND_MOUNT")); AppendMenuW (popup, MF_STRING, IDPM_SELECT_DEVICE_AND_MOUNT, GetString ("SELECT_DEVICE_AND_MOUNT")); break; @@ -5614,13 +5627,13 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa AppendMenuW (popup, MF_STRING, IDM_UNMOUNT_VOLUME, GetString ("DISMOUNT")); AppendMenuW (popup, MF_STRING, IDPM_OPEN_VOLUME, GetString ("OPEN")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDPM_CHECK_FILESYS, GetString ("IDPM_CHECK_FILESYS")); AppendMenuW (popup, MF_STRING, IDPM_REPAIR_FILESYS, GetString ("IDPM_REPAIR_FILESYS")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDPM_ADD_TO_FAVORITES, GetString ("IDPM_ADD_TO_FAVORITES")); AppendMenuW (popup, MF_STRING, IDPM_ADD_TO_SYSTEM_FAVORITES, GetString ("IDPM_ADD_TO_SYSTEM_FAVORITES")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_VOLUME_PROPERTIES, GetString ("IDPM_PROPERTIES")); break; @@ -5867,10 +5880,10 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa { AppendMenuW (popup, MF_STRING, IDM_CHANGE_PASSWORD, GetString ("IDM_CHANGE_PASSWORD")); AppendMenuW (popup, MF_STRING, IDM_CHANGE_HEADER_KEY_DERIV_ALGO, GetString ("IDM_CHANGE_HEADER_KEY_DERIV_ALGO")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_ADD_REMOVE_VOL_KEYFILES, GetString ("IDM_ADD_REMOVE_VOL_KEYFILES")); AppendMenuW (popup, MF_STRING, IDM_REMOVE_ALL_KEYFILES_FROM_VOL, GetString ("IDM_REMOVE_ALL_KEYFILES_FROM_VOL")); - AppendMenu (popup, MF_SEPARATOR, 0, NULL); + AppendMenu (popup, MF_SEPARATOR, 0, ""); AppendMenuW (popup, MF_STRING, IDM_BACKUP_VOL_HEADER, GetString ("IDM_BACKUP_VOL_HEADER")); AppendMenuW (popup, MF_STRING, IDM_RESTORE_VOL_HEADER, GetString ("IDM_RESTORE_VOL_HEADER")); } @@ -6250,9 +6263,9 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa // volPathHigher will contain the volume path selected in the main drive list wstring volPathHigher (prop.wszVolume); - ToSBCS (prop.wszVolume); - strcpy ((char *) volPathLowerW, volPathLower); - ToUNICODE ((char *) volPathLowerW); + ToSBCS (prop.wszVolume, sizeof(prop.wszVolume)); + StringCbCopyA ((char *) volPathLowerW, sizeof(volPathLowerW), volPathLower); + ToUNICODE ((char *) volPathLowerW, sizeof(volPathLowerW)); if (strcmp (((memcmp ((char *) prop.wszVolume, "\\??\\", 4) == 0) ? (char *) prop.wszVolume + 4 : (char *) prop.wszVolume), volPathLower) != 0) { @@ -6700,8 +6713,11 @@ void ExtractCommandLine (HWND hwndDlg, char *lpszCommandLine) KeyFile *kf; RelativePath2Absolute (tmpPath); kf = (KeyFile *) malloc (sizeof (KeyFile)); - strncpy (kf->FileName, tmpPath, sizeof (kf->FileName) - 1); - FirstCmdKeyFile = KeyFileAdd (FirstCmdKeyFile, kf); + if (kf) + { + StringCbCopyA (kf->FileName, sizeof(kf->FileName), tmpPath); + FirstCmdKeyFile = KeyFileAdd (FirstCmdKeyFile, kf); + } } break; @@ -7025,7 +7041,7 @@ BOOL TaskBarIconAdd (HWND hwnd) | LR_SHARED | (nCurrentOS != WIN_2000 ? LR_DEFAULTCOLOR : LR_VGACOLOR)); // Windows 2000 cannot display more than 16 fixed colors in notification tray - wcscpy (tnid.szTip, L"VeraCrypt"); + StringCbCopyW (tnid.szTip, sizeof(tnid.szTip), L"VeraCrypt"); return Shell_NotifyIconW (NIM_ADD, &tnid); } @@ -7450,7 +7466,7 @@ void ChangeMainWindowVisibility () } -int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume) +int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, const char *lpszVolume) { int nStatus = ERR_OS_ERROR; wchar_t szTmp[4096]; @@ -7461,6 +7477,13 @@ int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolum byte temporaryKey[MASTER_KEYDATA_SIZE]; byte originalK2[MASTER_KEYDATA_SIZE]; + if (!lpszVolume) + { + nStatus = ERR_OUTOFMEMORY; + handleError (hwndDlg, nStatus); + return nStatus; + } + volume.VolumeIsOpen = FALSE; hiddenVolume.VolumeIsOpen = FALSE; @@ -7560,7 +7583,7 @@ noHidden: goto error; } - swprintf (szTmp, GetString ("CONFIRM_VOL_HEADER_BAK"), lpszVolume); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("CONFIRM_VOL_HEADER_BAK"), lpszVolume); if (bRequireConfirmation && (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONQUESTION|MB_DEFBUTTON1) == IDNO)) @@ -7672,7 +7695,7 @@ error: } -int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume) +int RestoreVolumeHeader (HWND hwndDlg, const char *lpszVolume) { int nDosLinkCreated = -1, nStatus = ERR_OS_ERROR; char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH]; @@ -7691,6 +7714,13 @@ int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume) LARGE_INTEGER headerOffset; CRYPTO_INFO *restoredCryptoInfo = NULL; + if (!lpszVolume) + { + nStatus = ERR_OUTOFMEMORY; + handleError (hwndDlg, nStatus); + return nStatus; + } + switch (IsSystemDevicePath (lpszVolume, hwndDlg, TRUE)) { case 1: @@ -7750,7 +7780,7 @@ int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume) // Open the volume using backup header while (TRUE) { - strncpy (PasswordDlgVolume, lpszVolume, sizeof (PasswordDlgVolume) - 1); + StringCbCopyA (PasswordDlgVolume, sizeof(PasswordDlgVolume), lpszVolume); if (!AskVolumePassword (hwndDlg, &VolumePassword, NULL, FALSE)) { nStatus = ERR_SUCCESS; @@ -7806,7 +7836,7 @@ int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume) { // Restore header from an external backup - swprintf (szTmp, GetString ("CONFIRM_VOL_HEADER_RESTORE"), lpszVolume); + StringCbPrintfW (szTmp, sizeof(szTmp), GetString ("CONFIRM_VOL_HEADER_RESTORE"), lpszVolume); if (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONWARNING|MB_DEFBUTTON2) == IDNO) { @@ -7837,13 +7867,13 @@ int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume) goto error; } - CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice); + CreateFullVolumePath (szDiskFile, sizeof(szDiskFile), lpszVolume, &bDevice); if (bDevice == FALSE) - strcpy (szCFDevice, szDiskFile); + StringCbCopyA (szCFDevice, sizeof(szCFDevice), szDiskFile); else { - nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE); + nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, sizeof(szDosDevice),szCFDevice, sizeof(szCFDevice),FALSE); if (nDosLinkCreated != 0) goto error; } @@ -8302,12 +8332,12 @@ static BOOL CALLBACK SecurityTokenPreferencesDlgProc (HWND hwndDlg, UINT msg, WP else { char prevSecurityTokenLibraryPath[MAX_PATH]; - strcpy (prevSecurityTokenLibraryPath, SecurityTokenLibraryPath); - strcpy (SecurityTokenLibraryPath, securityTokenLibraryPath); + StringCbCopyA (prevSecurityTokenLibraryPath, sizeof(prevSecurityTokenLibraryPath), SecurityTokenLibraryPath); + StringCbCopyA (SecurityTokenLibraryPath, sizeof(SecurityTokenLibraryPath), securityTokenLibraryPath); if (!InitSecurityTokenLibrary()) { - strcpy (SecurityTokenLibraryPath, prevSecurityTokenLibraryPath); + StringCbCopyA (SecurityTokenLibraryPath, sizeof(SecurityTokenLibraryPath), prevSecurityTokenLibraryPath); return 1; } } @@ -8393,7 +8423,7 @@ static BOOL CALLBACK SecurityTokenPreferencesDlgProc (HWND hwndDlg, UINT msg, WP Info ("SELECT_PKCS11_MODULE_HELP"); - wsprintfW (browseFilter, L"%ls (*.dll)%c*.dll%c%c", GetString ("DLL_FILES"), 0, 0, 0); + StringCbPrintfW (browseFilter, sizeof(browseFilter), L"%ls (*.dll)%c*.dll%c%c", GetString ("DLL_FILES"), 0, 0, 0); GetSystemDirectory (systemDir, sizeof (systemDir)); if (BrowseFilesInDir (hwndDlg, "SELECT_PKCS11_MODULE", systemDir, securityTokenLibraryPath, TRUE, FALSE, browseFilter)) diff --git a/src/Mount/Mount.h b/src/Mount/Mount.h index 140b3e99..7509542c 100644 --- a/src/Mount/Mount.h +++ b/src/Mount/Mount.h @@ -94,8 +94,8 @@ BOOL WholeSysDriveEncryption (BOOL bSilent); BOOL CheckSysEncMountWithoutPBA (const char *devicePath, BOOL quiet); BOOL TCBootLoaderOnInactiveSysEncDrive (void); void CreateRescueDisk (void); -int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume); -int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume); +int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, const char *lpszVolume); +int RestoreVolumeHeader (HWND hwndDlg, const char *lpszVolume); void SecurityTokenPreferencesDialog (HWND hwndDlg); static BOOL CALLBACK PerformanceSettingsDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam); static BOOL CALLBACK BootLoaderPreferencesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam); -- cgit v1.2.3