diff options
author | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2014-07-14 17:41:09 +0200 |
---|---|---|
committer | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2014-11-08 23:21:27 +0100 |
commit | c220db01281564bf5b50575ee7e24b38e45f5050 (patch) | |
tree | 5e66aa935ec029ca2bac6fa282f4c18710fc2d0d /src/Common/Keyfiles.c | |
parent | c01f392a7ba1d5cdd4aa182eeb273cf41717d94f (diff) | |
download | VeraCrypt-c220db01281564bf5b50575ee7e24b38e45f5050.tar.gz VeraCrypt-c220db01281564bf5b50575ee7e24b38e45f5050.zip |
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.
Diffstat (limited to 'src/Common/Keyfiles.c')
-rw-r--r-- | src/Common/Keyfiles.c | 109 |
1 files changed, 66 insertions, 43 deletions
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 @@ | |||
23 | #include "Platform/Finally.h" | 23 | #include "Platform/Finally.h" |
24 | #include "Platform/ForEach.h" | 24 | #include "Platform/ForEach.h" |
25 | 25 | ||
26 | #include <Strsafe.h> | ||
27 | |||
26 | using namespace VeraCrypt; | 28 | using namespace VeraCrypt; |
27 | 29 | ||
28 | #define stat _stat | 30 | #define stat _stat |
29 | #define S_IFDIR _S_IFDIR | 31 | #define S_IFDIR _S_IFDIR |
30 | #define snprintf _snprintf | ||
31 | 32 | ||
32 | 33 | ||
33 | BOOL HiddenFilesPresentInKeyfilePath = FALSE; | 34 | BOOL HiddenFilesPresentInKeyfilePath = FALSE; |
@@ -97,13 +98,16 @@ void KeyFileRemoveAll (KeyFile **firstKeyFile) | |||
97 | 98 | ||
98 | KeyFile *KeyFileClone (KeyFile *keyFile) | 99 | KeyFile *KeyFileClone (KeyFile *keyFile) |
99 | { | 100 | { |
100 | KeyFile *clone; | 101 | KeyFile *clone = NULL; |
101 | 102 | ||
102 | if (keyFile == NULL) return NULL; | 103 | if (keyFile == NULL) return NULL; |
103 | 104 | ||
104 | clone = (KeyFile *) malloc (sizeof (KeyFile)); | 105 | clone = (KeyFile *) malloc (sizeof (KeyFile)); |
105 | strcpy (clone->FileName, keyFile->FileName); | 106 | if (clone) |
106 | clone->Next = NULL; | 107 | { |
108 | StringCbCopyA (clone->FileName, sizeof(clone->FileName), keyFile->FileName); | ||
109 | clone->Next = NULL; | ||
110 | } | ||
107 | return clone; | 111 | return clone; |
108 | } | 112 | } |
109 | 113 | ||
@@ -298,7 +302,7 @@ BOOL KeyFilesApply (Password *password, KeyFile *firstKeyFile) | |||
298 | /* Find and process all keyfiles in the directory */ | 302 | /* Find and process all keyfiles in the directory */ |
299 | int keyfileCount = 0; | 303 | int keyfileCount = 0; |
300 | 304 | ||
301 | snprintf (searchPath, sizeof (searchPath), "%s\\*.*", kf->FileName); | 305 | StringCbPrintfA (searchPath, sizeof (searchPath), "%s\\*.*", kf->FileName); |
302 | if ((searchHandle = _findfirst (searchPath, &fBuf)) == -1) | 306 | if ((searchHandle = _findfirst (searchPath, &fBuf)) == -1) |
303 | { | 307 | { |
304 | handleWin32Error (MainDlg); | 308 | handleWin32Error (MainDlg); |
@@ -311,7 +315,7 @@ BOOL KeyFilesApply (Password *password, KeyFile *firstKeyFile) | |||
311 | { | 315 | { |
312 | WIN32_FILE_ATTRIBUTE_DATA fileAttributes; | 316 | WIN32_FILE_ATTRIBUTE_DATA fileAttributes; |
313 | 317 | ||
314 | snprintf (kfSub->FileName, sizeof(kfSub->FileName), "%s%c%s", kf->FileName, | 318 | StringCbPrintfA (kfSub->FileName, sizeof(kfSub->FileName), "%s%c%s", kf->FileName, |
315 | '\\', | 319 | '\\', |
316 | fBuf.name | 320 | fBuf.name |
317 | ); | 321 | ); |
@@ -462,18 +466,21 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa | |||
462 | if (lw == IDC_KEYADD) | 466 | if (lw == IDC_KEYADD) |
463 | { | 467 | { |
464 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 468 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
465 | if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, bHistory)) | 469 | if (kf) |
466 | { | 470 | { |
467 | do | 471 | if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, sizeof(kf->FileName),bHistory)) |
468 | { | 472 | { |
469 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 473 | do |
470 | LoadKeyList (hwndDlg, param->FirstKeyFile); | 474 | { |
475 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | ||
476 | LoadKeyList (hwndDlg, param->FirstKeyFile); | ||
471 | 477 | ||
472 | kf = (KeyFile *) malloc (sizeof (KeyFile)); | 478 | kf = (KeyFile *) malloc (sizeof (KeyFile)); |
473 | } while (SelectMultipleFilesNext (kf->FileName)); | 479 | } while (SelectMultipleFilesNext (kf->FileName, sizeof(kf->FileName))); |
474 | } | 480 | } |
475 | 481 | ||
476 | free (kf); | 482 | free (kf); |
483 | } | ||
477 | return 1; | 484 | return 1; |
478 | } | 485 | } |
479 | 486 | ||
@@ -501,10 +508,13 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa | |||
501 | foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) | 508 | foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) |
502 | { | 509 | { |
503 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 510 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
504 | strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); | 511 | if (kf) |
512 | { | ||
513 | strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); | ||
505 | 514 | ||
506 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 515 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); |
507 | LoadKeyList (hwndDlg, param->FirstKeyFile); | 516 | LoadKeyList (hwndDlg, param->FirstKeyFile); |
517 | } | ||
508 | } | 518 | } |
509 | } | 519 | } |
510 | 520 | ||
@@ -574,9 +584,12 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa | |||
574 | while (count-- > 0) | 584 | while (count-- > 0) |
575 | { | 585 | { |
576 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 586 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
577 | DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); | 587 | if (kf) |
578 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 588 | { |
579 | LoadKeyList (hwndDlg, param->FirstKeyFile); | 589 | DragQueryFile (hdrop, i++, kf->FileName, sizeof (kf->FileName)); |
590 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | ||
591 | LoadKeyList (hwndDlg, param->FirstKeyFile); | ||
592 | } | ||
580 | } | 593 | } |
581 | 594 | ||
582 | DragFinish (hdrop); | 595 | DragFinish (hdrop); |
@@ -614,6 +627,8 @@ BOOL CALLBACK KeyFilesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa | |||
614 | BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *param) | 627 | BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *param) |
615 | { | 628 | { |
616 | HMENU popup = CreatePopupMenu (); | 629 | HMENU popup = CreatePopupMenu (); |
630 | if (!popup) | ||
631 | return FALSE; | ||
617 | int sel; | 632 | int sel; |
618 | BOOL status = FALSE; | 633 | BOOL status = FALSE; |
619 | 634 | ||
@@ -628,35 +643,40 @@ BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *par | |||
628 | case IDM_KEYFILES_POPUP_ADD_FILES: | 643 | case IDM_KEYFILES_POPUP_ADD_FILES: |
629 | { | 644 | { |
630 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 645 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
631 | if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, bHistory)) | 646 | if (kf) |
632 | { | 647 | { |
633 | do | 648 | if (SelectMultipleFiles (hwndDlg, "SELECT_KEYFILE", kf->FileName, sizeof(kf->FileName),bHistory)) |
634 | { | 649 | { |
635 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 650 | do |
636 | kf = (KeyFile *) malloc (sizeof (KeyFile)); | 651 | { |
637 | } while (SelectMultipleFilesNext (kf->FileName)); | 652 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); |
653 | kf = (KeyFile *) malloc (sizeof (KeyFile)); | ||
654 | } while (SelectMultipleFilesNext (kf->FileName, sizeof(kf->FileName))); | ||
638 | 655 | ||
639 | param->EnableKeyFiles = TRUE; | 656 | param->EnableKeyFiles = TRUE; |
640 | status = TRUE; | 657 | status = TRUE; |
641 | } | 658 | } |
642 | 659 | ||
643 | free (kf); | 660 | free (kf); |
661 | } | ||
644 | } | 662 | } |
645 | break; | 663 | break; |
646 | 664 | ||
647 | case IDM_KEYFILES_POPUP_ADD_DIR: | 665 | case IDM_KEYFILES_POPUP_ADD_DIR: |
648 | { | 666 | { |
649 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 667 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
650 | 668 | if (kf) | |
651 | if (BrowseDirectories (hwndDlg,"SELECT_KEYFILE_PATH", kf->FileName)) | ||
652 | { | 669 | { |
653 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 670 | if (BrowseDirectories (hwndDlg,"SELECT_KEYFILE_PATH", kf->FileName)) |
654 | param->EnableKeyFiles = TRUE; | 671 | { |
655 | status = TRUE; | 672 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); |
656 | } | 673 | param->EnableKeyFiles = TRUE; |
657 | else | 674 | status = TRUE; |
658 | { | 675 | } |
659 | free (kf); | 676 | else |
677 | { | ||
678 | free (kf); | ||
679 | } | ||
660 | } | 680 | } |
661 | } | 681 | } |
662 | break; | 682 | break; |
@@ -669,11 +689,14 @@ BOOL KeyfilesPopupMenu (HWND hwndDlg, POINT popupPosition, KeyFilesDlgParam *par | |||
669 | foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) | 689 | foreach (const SecurityTokenKeyfilePath &keyPath, selectedTokenKeyfiles) |
670 | { | 690 | { |
671 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); | 691 | KeyFile *kf = (KeyFile *) malloc (sizeof (KeyFile)); |
672 | strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); | 692 | if (kf) |
673 | 693 | { | |
674 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); | 694 | strcpy_s (kf->FileName, sizeof (kf->FileName), WideToSingleString (keyPath).c_str()); |
675 | param->EnableKeyFiles = TRUE; | 695 | |
676 | status = TRUE; | 696 | param->FirstKeyFile = KeyFileAdd (param->FirstKeyFile, kf); |
697 | param->EnableKeyFiles = TRUE; | ||
698 | status = TRUE; | ||
699 | } | ||
677 | } | 700 | } |
678 | } | 701 | } |
679 | } | 702 | } |