Re: [edk2] [patch] MdeModulePkg:Use safe string functions in UiApp.
Reviewed-by: Qiu Shumin shumin@intel.com -Original Message- From: Bi, Dandan Sent: Tuesday, August 11, 2015 11:07 AM To: Dong, Eric; Qiu, Shumin; edk2-devel@lists.01.org Subject: [patch] MdeModulePkg:Use safe string functions in UiApp. Replace the unsafe string functions with the safe one in UiApp. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Dandan Bi dandan...@intel.com --- .../Application/UiApp/BootMaint/BootOption.c | 30 ++ .../Application/UiApp/BootMaint/FormGuid.h | 2 +- .../Application/UiApp/BootMaint/UpdatePage.c | 2 +- .../Application/UiApp/BootMaint/Variable.c | 4 +-- .../Application/UiApp/BootMngr/BootManager.c | 6 +++-- .../Application/UiApp/DeviceMngr/DeviceManager.c | 2 +- MdeModulePkg/Application/UiApp/FrontPage.c | 13 +- 7 files changed, 30 insertions(+), 29 deletions(-) diff --git a/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c b/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c index fa88f63..5213e3c 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c +++ b/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c @@ -784,11 +784,11 @@ BOpt_GetBootOptions ( StringSize = StrSize((UINT16*)LoadOptionPtr); NewLoadContext-Description = AllocateZeroPool (StrSize((UINT16*)LoadOptionPtr)); ASSERT (NewLoadContext-Description != NULL); -StrCpy (NewLoadContext-Description, (UINT16*)LoadOptionPtr); +StrCpyS (NewLoadContext-Description, + StrSize((UINT16*)LoadOptionPtr) / sizeof (UINT16), + (UINT16*)LoadOptionPtr); ASSERT (NewLoadContext-Description != NULL); NewMenuEntry-DisplayString = NewLoadContext-Description; LoadOptionPtr += StringSize; @@ -860,31 +860,29 @@ CHAR16 * BOpt_AppendFileName ( IN CHAR16 *Str1, IN CHAR16 *Str2 ) { - UINTN Size1; - UINTN Size2; + UINTN DestMax; CHAR16 *Str; CHAR16 *TmpStr; CHAR16 *Ptr; CHAR16 *LastSlash; - Size1 = StrSize (Str1); - Size2 = StrSize (Str2); - Str = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); + DestMax = (StrSize (Str1) + StrSize (Str2) + sizeof (CHAR16)) / sizeof (CHAR16); + Str = AllocateZeroPool (DestMax * sizeof (CHAR16)); ASSERT (Str != NULL); - TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); + TmpStr = AllocateZeroPool (DestMax * sizeof (CHAR16)); ASSERT (TmpStr != NULL); - StrCat (Str, Str1); + StrCatS (Str, DestMax, Str1); if (!((*Str == '\\') (*(Str + 1) == 0))) { -StrCat (Str, L\\); +StrCatS (Str, DestMax, L\\); } - StrCat (Str, Str2); + StrCatS (Str, DestMax, Str2); Ptr = Str; LastSlash = Str; while (*Ptr != 0) { if (*Ptr == '\\' *(Ptr + 1) == '.' *(Ptr + 2) == '.' *(Ptr + 3) == L'\\') { @@ -893,27 +891,27 @@ BOpt_AppendFileName ( // DO NOT convert the .. if it is at the end of the string. This will // break the .. behavior in changing directories. // // - // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings + // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle + copy of two strings // that overlap. // - StrCpy (TmpStr, Ptr + 3); - StrCpy (LastSlash, TmpStr); + StrCpyS (TmpStr, DestMax, Ptr + 3); + StrCpyS (LastSlash, DestMax - (UINTN) (LastSlash - Str), TmpStr); Ptr = LastSlash; } else if (*Ptr == '\\' *(Ptr + 1) == '.' *(Ptr + 2) == '\\') { // // Convert a \.\ to a \ // // - // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings + // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle + copy of two strings // that overlap. // - StrCpy (TmpStr, Ptr + 2); - StrCpy (Ptr, TmpStr); + StrCpyS (TmpStr, DestMax, Ptr + 2); + StrCpyS (Ptr, DestMax - (UINTN) (Ptr - Str), TmpStr); Ptr = LastSlash; } else if (*Ptr == '\\') { LastSlash = Ptr; } diff --git a/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h b/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h index c80d792..ab3d9c9 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h +++ b/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h @@ -193,11 +193,11 @@ typedef struct { /// /// This is the data structure used by File Explorer formset /// typedef struct { - UINT16 DescriptionData[75]; + UINT16 DescriptionData[MAX_MENU_NUMBER]; UINT16 OptionalData[127]; UINT8 Active; UINT8 ForceReconnect; } FILE_EXPLORER_NV_DATA; diff --git a/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c b/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c index d85f2ea..0e85a83 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c +++ b/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c @@ -836,11 +836,11 @@ UpdateConModePage ( // // Build
[edk2] [patch] MdeModulePkg:Use safe string functions in UiApp.
Replace the unsafe string functions with the safe one in UiApp. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Dandan Bi dandan...@intel.com --- .../Application/UiApp/BootMaint/BootOption.c | 30 ++ .../Application/UiApp/BootMaint/FormGuid.h | 2 +- .../Application/UiApp/BootMaint/UpdatePage.c | 2 +- .../Application/UiApp/BootMaint/Variable.c | 4 +-- .../Application/UiApp/BootMngr/BootManager.c | 6 +++-- .../Application/UiApp/DeviceMngr/DeviceManager.c | 2 +- MdeModulePkg/Application/UiApp/FrontPage.c | 13 +- 7 files changed, 30 insertions(+), 29 deletions(-) diff --git a/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c b/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c index fa88f63..5213e3c 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c +++ b/MdeModulePkg/Application/UiApp/BootMaint/BootOption.c @@ -784,11 +784,11 @@ BOpt_GetBootOptions ( StringSize = StrSize((UINT16*)LoadOptionPtr); NewLoadContext-Description = AllocateZeroPool (StrSize((UINT16*)LoadOptionPtr)); ASSERT (NewLoadContext-Description != NULL); -StrCpy (NewLoadContext-Description, (UINT16*)LoadOptionPtr); +StrCpyS (NewLoadContext-Description, StrSize((UINT16*)LoadOptionPtr) / sizeof (UINT16), (UINT16*)LoadOptionPtr); ASSERT (NewLoadContext-Description != NULL); NewMenuEntry-DisplayString = NewLoadContext-Description; LoadOptionPtr += StringSize; @@ -860,31 +860,29 @@ CHAR16 * BOpt_AppendFileName ( IN CHAR16 *Str1, IN CHAR16 *Str2 ) { - UINTN Size1; - UINTN Size2; + UINTN DestMax; CHAR16 *Str; CHAR16 *TmpStr; CHAR16 *Ptr; CHAR16 *LastSlash; - Size1 = StrSize (Str1); - Size2 = StrSize (Str2); - Str = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); + DestMax = (StrSize (Str1) + StrSize (Str2) + sizeof (CHAR16)) / sizeof (CHAR16); + Str = AllocateZeroPool (DestMax * sizeof (CHAR16)); ASSERT (Str != NULL); - TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); + TmpStr = AllocateZeroPool (DestMax * sizeof (CHAR16)); ASSERT (TmpStr != NULL); - StrCat (Str, Str1); + StrCatS (Str, DestMax, Str1); if (!((*Str == '\\') (*(Str + 1) == 0))) { -StrCat (Str, L\\); +StrCatS (Str, DestMax, L\\); } - StrCat (Str, Str2); + StrCatS (Str, DestMax, Str2); Ptr = Str; LastSlash = Str; while (*Ptr != 0) { if (*Ptr == '\\' *(Ptr + 1) == '.' *(Ptr + 2) == '.' *(Ptr + 3) == L'\\') { @@ -893,27 +891,27 @@ BOpt_AppendFileName ( // DO NOT convert the .. if it is at the end of the string. This will // break the .. behavior in changing directories. // // - // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings + // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings // that overlap. // - StrCpy (TmpStr, Ptr + 3); - StrCpy (LastSlash, TmpStr); + StrCpyS (TmpStr, DestMax, Ptr + 3); + StrCpyS (LastSlash, DestMax - (UINTN) (LastSlash - Str), TmpStr); Ptr = LastSlash; } else if (*Ptr == '\\' *(Ptr + 1) == '.' *(Ptr + 2) == '\\') { // // Convert a \.\ to a \ // // - // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings + // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings // that overlap. // - StrCpy (TmpStr, Ptr + 2); - StrCpy (Ptr, TmpStr); + StrCpyS (TmpStr, DestMax, Ptr + 2); + StrCpyS (Ptr, DestMax - (UINTN) (Ptr - Str), TmpStr); Ptr = LastSlash; } else if (*Ptr == '\\') { LastSlash = Ptr; } diff --git a/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h b/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h index c80d792..ab3d9c9 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h +++ b/MdeModulePkg/Application/UiApp/BootMaint/FormGuid.h @@ -193,11 +193,11 @@ typedef struct { /// /// This is the data structure used by File Explorer formset /// typedef struct { - UINT16 DescriptionData[75]; + UINT16 DescriptionData[MAX_MENU_NUMBER]; UINT16 OptionalData[127]; UINT8 Active; UINT8 ForceReconnect; } FILE_EXPLORER_NV_DATA; diff --git a/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c b/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c index d85f2ea..0e85a83 100644 --- a/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c +++ b/MdeModulePkg/Application/UiApp/BootMaint/UpdatePage.c @@ -836,11 +836,11 @@ UpdateConModePage ( // // Build mode string Column x Row // UnicodeValueToString (ModeString, 0, Col, 0); PStr = ModeString[0]; -StrnCat (PStr, L x , StrLen(L x ) + 1); +StrnCatS (PStr, sizeof (ModeString) / sizeof (ModeString[0]), L x , StrLen(L x ) + 1); PStr =