[PATCH] UefiPayloadPkg: Fix Coverity report defect


GregX Yeh
 

From: Gregx Yeh <gregx.yeh@...>

https://bugzilla.tianocore.org/show_bug.cgi?id=3D4018
Coverity report FORWARD_NULL and OVERFLOW_BEFORE_WIDEN potential defect
in UefiPayloadPkg.

Signed-off-by: Gregx Yeh <gregx.yeh@...>
---
UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c | 4 ++--
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 2 +-
UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c b/U=
efiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
index 9847063d3d..790e6109c0 100644
--- a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
+++ b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
@@ -232,11 +232,11 @@ PciHostBridgeFreeRootBridges (
UINTN Count=0D
)=0D
{=0D
- if ((Bridges =3D=3D NULL) && (Count =3D=3D 0)) {=0D
+ if ((Bridges =3D=3D NULL) || (Count =3D=3D 0)) {=0D
return;=0D
}=0D
=0D
- ASSERT (Bridges !=3D NULL && Count > 0);=0D
+ ASSERT (Bridges !=3D NULL || Count > 0);=0D
=0D
do {=0D
--Count;=0D
diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayload=
Pkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..a9bf6f3223 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -149,7 +149,7 @@ CalculateElfFileSize (
FileSize2 =3D Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_=
shnum;=0D
} else if (ElfCt->EiClass =3D=3D ELFCLASS64) {=0D
Elf64Hdr =3D (Elf64_Ehdr *)ElfCt->FileBase;=0D
- FileSize2 =3D (UINTN)(Elf64Hdr->e_shoff + Elf64Hdr->e_shentsize * Elf6=
4Hdr->e_shnum);=0D
+ FileSize2 =3D ((UINTN)Elf64Hdr->e_shoff + (UINTN)(Elf64Hdr->e_shentsiz=
e * Elf64Hdr->e_shnum));=0D
}=0D
=0D
*FileSize =3D MAX (FileSize1, FileSize2);=0D
diff --git a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c b/UefiPayl=
oadPkg/UefiPayloadEntry/X64/VirtualMemory.c
index 74b667a62a..8d8a9d0ca8 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
@@ -783,9 +783,9 @@ CreateIdentityMappingPageTables (
// Pre-allocate big pages to avoid later allocations.=0D
//=0D
if (!Page1GSupport) {=0D
- TotalPagesNum =3D ((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4Entrie=
sNeeded + 1) * NumberOfPml5EntriesNeeded + 1;=0D
+ TotalPagesNum =3D (UINTN)((NumberOfPdpEntriesNeeded + 1) * NumberOfPml=
4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;=0D
} else {=0D
- TotalPagesNum =3D (NumberOfPml4EntriesNeeded + 1) * NumberOfPml5Entrie=
sNeeded + 1;=0D
+ TotalPagesNum =3D (UINTN)(NumberOfPml4EntriesNeeded + 1) * NumberOfPml=
5EntriesNeeded + 1;=0D
}=0D
=0D
//=0D
--=20
2.26.2.windows.1


GregX Yeh
 

From: Gregx Yeh <gregx.yeh@...>

https://bugzilla.tianocore.org/show_bug.cgi?id=3D4018
Coverity report FORWARD_NULL and OVERFLOW_BEFORE_WIDEN potential defect
in UefiPayloadPkg.

Signed-off-by: Gregx Yeh <gregx.yeh@...>
Cc: Guo Dong <guo.dong@...>
Cc: Ray Ni <ray.ni@...>
---
UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c | 4 ++--
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 2 +-
UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c b/U=
efiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
index 9847063d3d..790e6109c0 100644
--- a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
+++ b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
@@ -232,11 +232,11 @@ PciHostBridgeFreeRootBridges (
UINTN Count=0D
)=0D
{=0D
- if ((Bridges =3D=3D NULL) && (Count =3D=3D 0)) {=0D
+ if ((Bridges =3D=3D NULL) || (Count =3D=3D 0)) {=0D
return;=0D
}=0D
=0D
- ASSERT (Bridges !=3D NULL && Count > 0);=0D
+ ASSERT (Bridges !=3D NULL || Count > 0);=0D
=0D
do {=0D
--Count;=0D
diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayload=
Pkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..a9bf6f3223 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -149,7 +149,7 @@ CalculateElfFileSize (
FileSize2 =3D Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_=
shnum;=0D
} else if (ElfCt->EiClass =3D=3D ELFCLASS64) {=0D
Elf64Hdr =3D (Elf64_Ehdr *)ElfCt->FileBase;=0D
- FileSize2 =3D (UINTN)(Elf64Hdr->e_shoff + Elf64Hdr->e_shentsize * Elf6=
4Hdr->e_shnum);=0D
+ FileSize2 =3D ((UINTN)Elf64Hdr->e_shoff + (UINTN)(Elf64Hdr->e_shentsiz=
e * Elf64Hdr->e_shnum));=0D
}=0D
=0D
*FileSize =3D MAX (FileSize1, FileSize2);=0D
diff --git a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c b/UefiPayl=
oadPkg/UefiPayloadEntry/X64/VirtualMemory.c
index 74b667a62a..8d8a9d0ca8 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
@@ -783,9 +783,9 @@ CreateIdentityMappingPageTables (
// Pre-allocate big pages to avoid later allocations.=0D
//=0D
if (!Page1GSupport) {=0D
- TotalPagesNum =3D ((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4Entrie=
sNeeded + 1) * NumberOfPml5EntriesNeeded + 1;=0D
+ TotalPagesNum =3D (UINTN)((NumberOfPdpEntriesNeeded + 1) * NumberOfPml=
4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;=0D
} else {=0D
- TotalPagesNum =3D (NumberOfPml4EntriesNeeded + 1) * NumberOfPml5Entrie=
sNeeded + 1;=0D
+ TotalPagesNum =3D (UINTN)(NumberOfPml4EntriesNeeded + 1) * NumberOfPml=
5EntriesNeeded + 1;=0D
}=0D
=0D
//=0D
--=20
2.26.2.windows.1


Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of GregX Yeh
Sent: Wednesday, August 10, 2022 11:46 PM
To: devel@edk2.groups.io
Cc: Yeh, GregX <gregx.yeh@...>; Dong, Guo <guo.dong@...>; Ni, Ray <ray.ni@...>
Subject: [edk2-devel] [PATCH] UefiPayloadPkg: Fix Coverity report defect

From: Gregx Yeh <gregx.yeh@...>

https://bugzilla.tianocore.org/show_bug.cgi?id=4018
Coverity report FORWARD_NULL and OVERFLOW_BEFORE_WIDEN potential defect in UefiPayloadPkg.

Signed-off-by: Gregx Yeh <gregx.yeh@...>
Cc: Guo Dong <guo.dong@...>
Cc: Ray Ni <ray.ni@...>
---
UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c | 4 ++--
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 2 +-
UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
index 9847063d3d..790e6109c0 100644
--- a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
+++ b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
@@ -232,11 +232,11 @@ PciHostBridgeFreeRootBridges (
UINTN Count ) {- if ((Bridges == NULL) && (Count == 0)) {+ if ((Bridges == NULL) || (Count == 0)) { return; } - ASSERT (Bridges != NULL && Count > 0);+ ASSERT (Bridges != NULL || Count > 0); do { --Count;diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..a9bf6f3223 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -149,7 +149,7 @@ CalculateElfFileSize (
FileSize2 = Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_shnum; } else if (ElfCt->EiClass == ELFCLASS64) { Elf64Hdr = (Elf64_Ehdr *)ElfCt->FileBase;- FileSize2 = (UINTN)(Elf64Hdr->e_shoff + Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum);+ FileSize2 = ((UINTN)Elf64Hdr->e_shoff + (UINTN)(Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum)); } *FileSize = MAX (FileSize1, FileSize2);diff --git a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
index 74b667a62a..8d8a9d0ca8 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
@@ -783,9 +783,9 @@ CreateIdentityMappingPageTables (
// Pre-allocate big pages to avoid later allocations. // if (!Page1GSupport) {- TotalPagesNum = ((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;+ TotalPagesNum = (UINTN)((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1; } else {- TotalPagesNum = (NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;+ TotalPagesNum = (UINTN)(NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1; } //--
2.26.2.windows.1



-=-=-=-=-=-=
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#92343): https://edk2.groups.io/g/devel/message/92343
Mute This Topic: https://groups.io/mt/92953360/1781375
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [guo.dong@...] -=-=-=-=-=-=


Lu, James
 

Reviewed-by: James Lu <james.lu@...>


Thanks,
James

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of GregX Yeh
Sent: Thursday, August 11, 2022 2:29 PM
To: devel@edk2.groups.io
Cc: Yeh, GregX <gregx.yeh@...>
Subject: [edk2-devel] [PATCH] UefiPayloadPkg: Fix Coverity report defect

From: Gregx Yeh <gregx.yeh@...>

https://bugzilla.tianocore.org/show_bug.cgi?id=4018
Coverity report FORWARD_NULL and OVERFLOW_BEFORE_WIDEN potential defect in UefiPayloadPkg.

Signed-off-by: Gregx Yeh <gregx.yeh@...>
---
UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c | 4 ++--
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 2 +-
UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
index 9847063d3d..790e6109c0 100644
--- a/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
+++ b/UefiPayloadPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
@@ -232,11 +232,11 @@ PciHostBridgeFreeRootBridges (
UINTN Count ) {- if ((Bridges == NULL) && (Count == 0)) {+ if ((Bridges == NULL) || (Count == 0)) { return; } - ASSERT (Bridges != NULL && Count > 0);+ ASSERT (Bridges != NULL || Count > 0); do { --Count;diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..a9bf6f3223 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -149,7 +149,7 @@ CalculateElfFileSize (
FileSize2 = Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_shnum; } else if (ElfCt->EiClass == ELFCLASS64) { Elf64Hdr = (Elf64_Ehdr *)ElfCt->FileBase;- FileSize2 = (UINTN)(Elf64Hdr->e_shoff + Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum);+ FileSize2 = ((UINTN)Elf64Hdr->e_shoff + (UINTN)(Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum)); } *FileSize = MAX (FileSize1, FileSize2);diff --git a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
index 74b667a62a..8d8a9d0ca8 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/X64/VirtualMemory.c
@@ -783,9 +783,9 @@ CreateIdentityMappingPageTables (
// Pre-allocate big pages to avoid later allocations. // if (!Page1GSupport) {- TotalPagesNum = ((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;+ TotalPagesNum = (UINTN)((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1; } else {- TotalPagesNum = (NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;+ TotalPagesNum = (UINTN)(NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1; } //--
2.26.2.windows.1



-=-=-=-=-=-=
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#92341): https://edk2.groups.io/g/devel/message/92341
Mute This Topic: https://groups.io/mt/92953360/4822895
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [james.lu@...] -=-=-=-=-=-=