Discussion:
[edk2] [PATCH v2] NetworkPkg: Fix a bug that return type differs from the left one when assigned
Zhang Lubo
2015-07-10 06:10:33 UTC
Permalink
Version v2 includes using local variable to save intermediate variable to make codes easier readable.
Using ASSERT_EFI_ERROR instead of check the return status.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Zhang Lubo <***@intel.com>
---
NetworkPkg/Application/IfConfig6/IfConfig6.c | 2 +-
NetworkPkg/Mtftp6Dxe/Mtftp6Support.c | 59 +++++++++++++++++-----------
2 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/NetworkPkg/Application/IfConfig6/IfConfig6.c b/NetworkPkg/Application/IfConfig6/IfConfig6.c
index 4cec44f..8d464b8 100644
--- a/NetworkPkg/Application/IfConfig6/IfConfig6.c
+++ b/NetworkPkg/Application/IfConfig6/IfConfig6.c
@@ -125,11 +125,11 @@ SplitStrToList (
CHAR16 *Str;
CHAR16 *ArgStr;
ARG_LIST *ArgList;
ARG_LIST *ArgNode;

- if (*String == L'\0' || *String == NULL) {
+ if (String == NULL || *String == L'\0') {
return NULL;
}

//
// Copy the CONST string to a local copy.
diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
index 91680b2..20888e8 100644
--- a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
+++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
@@ -450,17 +450,20 @@ Mtftp6SendRequest (
)
{
EFI_MTFTP6_PACKET *Packet;
EFI_MTFTP6_OPTION *Options;
EFI_MTFTP6_TOKEN *Token;
+ RETURN_STATUS Status;
NET_BUF *Nbuf;
UINT8 *Mode;
UINT8 *Cur;
- UINT32 Len1;
- UINT32 Len2;
- UINT32 Len;
UINTN Index;
+ UINT32 BufferLength;
+ UINTN FileNameLength;
+ UINTN ModeLength;
+ UINTN OptionStrLength;
+ UINTN ValueStrLength;

Token = Instance->Token;
Options = Token->OptionList;
Mode = Token->ModeStr;

@@ -485,51 +488,63 @@ Mtftp6SendRequest (
//

//
// Compute the size of new Mtftp6 packet.
//
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);
- Len = Len1 + Len2 + 4;
+ FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename);
+ ModeLength = AsciiStrLen ((CHAR8 *) Mode);
+ BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4;

for (Index = 0; Index < Token->OptionCount; Index++) {
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
- Len += Len1 + Len2 + 2;
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+ BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2;
}

//
// Allocate a packet then copy the data.
//
- if ((Nbuf = NetbufAlloc (Len)) == NULL) {
+ if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {
return EFI_OUT_OF_RESOURCES;
}

//
// Copy the opcode, filename and mode into packet.
//
- Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
+ Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE);
ASSERT (Packet != NULL);

Packet->OpCode = HTONS (Operation);
+ BufferLength -= sizeof (Packet->OpCode);
+
Cur = Packet->Rrq.Filename;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2, (CHAR8 *) Token->Filename);
- Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2 - (AsciiStrLen ((CHAR8 *) Token->Filename) + 1), (CHAR8 *) Mode);
- Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;
- Len -= ((UINT32) AsciiStrLen ((CHAR8 *) Token->Filename) + (UINT32) AsciiStrLen ((CHAR8 *) Mode) + 4);
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (FileNameLength + 1);
+ Cur += FileNameLength + 1;
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ModeLength + 1);
+ Cur += ModeLength + 1;

//
// Copy all the extension options into the packet.
//
for (Index = 0; Index < Token->OptionCount; ++Index) {
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].OptionStr);
- Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1);
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].ValueStr);
- Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].ValueStr) + 1);
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (OptionStrLength + 1);
+ Cur += OptionStrLength + 1;
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ValueStrLength + 1);
+ Cur += ValueStrLength + 1;
+
}

//
// Save the packet buf for retransmit
//
--
1.9.5.msysgit.1
Fu, Siyuan
2015-07-10 06:26:37 UTC
Permalink
Reviewed-by: Fu Siyuan <***@intel.com>

-----Original Message-----
From: Zhang, Lubo
Sent: Friday, July 10, 2015 2:11 PM
To: edk2-***@lists.sourceforge.net; Fu, Siyuan; Qiu, Shumin; Wu, Jiaxin
Subject: [PATCH v2] NetworkPkg: Fix a bug that return type differs from the left one when assigned


Version v2 includes using local variable to save intermediate variable to make codes easier readable.
Using ASSERT_EFI_ERROR instead of check the return status.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Zhang Lubo <***@intel.com>
---
NetworkPkg/Application/IfConfig6/IfConfig6.c | 2 +-
NetworkPkg/Mtftp6Dxe/Mtftp6Support.c | 59 +++++++++++++++++-----------
2 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/NetworkPkg/Application/IfConfig6/IfConfig6.c b/NetworkPkg/Application/IfConfig6/IfConfig6.c
index 4cec44f..8d464b8 100644
--- a/NetworkPkg/Application/IfConfig6/IfConfig6.c
+++ b/NetworkPkg/Application/IfConfig6/IfConfig6.c
@@ -125,11 +125,11 @@ SplitStrToList (
CHAR16 *Str;
CHAR16 *ArgStr;
ARG_LIST *ArgList;
ARG_LIST *ArgNode;

- if (*String == L'\0' || *String == NULL) {
+ if (String == NULL || *String == L'\0') {
return NULL;
}

//
// Copy the CONST string to a local copy.
diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
index 91680b2..20888e8 100644
--- a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
+++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
@@ -450,17 +450,20 @@ Mtftp6SendRequest (
)
{
EFI_MTFTP6_PACKET *Packet;
EFI_MTFTP6_OPTION *Options;
EFI_MTFTP6_TOKEN *Token;
+ RETURN_STATUS Status;
NET_BUF *Nbuf;
UINT8 *Mode;
UINT8 *Cur;
- UINT32 Len1;
- UINT32 Len2;
- UINT32 Len;
UINTN Index;
+ UINT32 BufferLength;
+ UINTN FileNameLength;
+ UINTN ModeLength;
+ UINTN OptionStrLength;
+ UINTN ValueStrLength;

Token = Instance->Token;
Options = Token->OptionList;
Mode = Token->ModeStr;

@@ -485,51 +488,63 @@ Mtftp6SendRequest (
//

//
// Compute the size of new Mtftp6 packet.
//
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);
- Len = Len1 + Len2 + 4;
+ FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename);
+ ModeLength = AsciiStrLen ((CHAR8 *) Mode);
+ BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4;

for (Index = 0; Index < Token->OptionCount; Index++) {
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
- Len += Len1 + Len2 + 2;
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+ BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2;
}

//
// Allocate a packet then copy the data.
//
- if ((Nbuf = NetbufAlloc (Len)) == NULL) {
+ if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {
return EFI_OUT_OF_RESOURCES;
}

//
// Copy the opcode, filename and mode into packet.
//
- Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
+ Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE);
ASSERT (Packet != NULL);

Packet->OpCode = HTONS (Operation);
+ BufferLength -= sizeof (Packet->OpCode);
+
Cur = Packet->Rrq.Filename;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2, (CHAR8 *) Token->Filename);
- Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2 - (AsciiStrLen ((CHAR8 *) Token->Filename) + 1), (CHAR8 *) Mode);
- Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;
- Len -= ((UINT32) AsciiStrLen ((CHAR8 *) Token->Filename) + (UINT32) AsciiStrLen ((CHAR8 *) Mode) + 4);
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (FileNameLength + 1);
+ Cur += FileNameLength + 1;
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ModeLength + 1);
+ Cur += ModeLength + 1;

//
// Copy all the extension options into the packet.
//
for (Index = 0; Index < Token->OptionCount; ++Index) {
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].OptionStr);
- Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1);
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].ValueStr);
- Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].ValueStr) + 1);
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (OptionStrLength + 1);
+ Cur += OptionStrLength + 1;
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ValueStrLength + 1);
+ Cur += ValueStrLength + 1;
+
}

//
// Save the packet buf for retransmit
//
--
1.9.5.msysgit.1
Wu, Jiaxin
2015-07-10 07:41:05 UTC
Permalink
Reviewed-by: Jiaxin Wu <***@intel.com>


-----Original Message-----
From: Zhang, Lubo
Sent: Friday, July 10, 2015 2:11 PM
To: edk2-***@lists.sourceforge.net; Fu, Siyuan; Qiu, Shumin; Wu, Jiaxin
Subject: [PATCH v2] NetworkPkg: Fix a bug that return type differs from the left one when assigned


Version v2 includes using local variable to save intermediate variable to make codes easier readable.
Using ASSERT_EFI_ERROR instead of check the return status.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Zhang Lubo <***@intel.com>
---
NetworkPkg/Application/IfConfig6/IfConfig6.c | 2 +-
NetworkPkg/Mtftp6Dxe/Mtftp6Support.c | 59 +++++++++++++++++-----------
2 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/NetworkPkg/Application/IfConfig6/IfConfig6.c b/NetworkPkg/Application/IfConfig6/IfConfig6.c
index 4cec44f..8d464b8 100644
--- a/NetworkPkg/Application/IfConfig6/IfConfig6.c
+++ b/NetworkPkg/Application/IfConfig6/IfConfig6.c
@@ -125,11 +125,11 @@ SplitStrToList (
CHAR16 *Str;
CHAR16 *ArgStr;
ARG_LIST *ArgList;
ARG_LIST *ArgNode;

- if (*String == L'\0' || *String == NULL) {
+ if (String == NULL || *String == L'\0') {
return NULL;
}

//
// Copy the CONST string to a local copy.
diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
index 91680b2..20888e8 100644
--- a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
+++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
@@ -450,17 +450,20 @@ Mtftp6SendRequest (
)
{
EFI_MTFTP6_PACKET *Packet;
EFI_MTFTP6_OPTION *Options;
EFI_MTFTP6_TOKEN *Token;
+ RETURN_STATUS Status;
NET_BUF *Nbuf;
UINT8 *Mode;
UINT8 *Cur;
- UINT32 Len1;
- UINT32 Len2;
- UINT32 Len;
UINTN Index;
+ UINT32 BufferLength;
+ UINTN FileNameLength;
+ UINTN ModeLength;
+ UINTN OptionStrLength;
+ UINTN ValueStrLength;

Token = Instance->Token;
Options = Token->OptionList;
Mode = Token->ModeStr;

@@ -485,51 +488,63 @@ Mtftp6SendRequest (
//

//
// Compute the size of new Mtftp6 packet.
//
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);
- Len = Len1 + Len2 + 4;
+ FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename);
+ ModeLength = AsciiStrLen ((CHAR8 *) Mode);
+ BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4;

for (Index = 0; Index < Token->OptionCount; Index++) {
- Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
- Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
- Len += Len1 + Len2 + 2;
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+ BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2;
}

//
// Allocate a packet then copy the data.
//
- if ((Nbuf = NetbufAlloc (Len)) == NULL) {
+ if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {
return EFI_OUT_OF_RESOURCES;
}

//
// Copy the opcode, filename and mode into packet.
//
- Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
+ Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE);
ASSERT (Packet != NULL);

Packet->OpCode = HTONS (Operation);
+ BufferLength -= sizeof (Packet->OpCode);
+
Cur = Packet->Rrq.Filename;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2, (CHAR8 *) Token->Filename);
- Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2 - (AsciiStrLen ((CHAR8 *) Token->Filename) + 1), (CHAR8 *) Mode);
- Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;
- Len -= ((UINT32) AsciiStrLen ((CHAR8 *) Token->Filename) + (UINT32) AsciiStrLen ((CHAR8 *) Mode) + 4);
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (FileNameLength + 1);
+ Cur += FileNameLength + 1;
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ModeLength + 1);
+ Cur += ModeLength + 1;

//
// Copy all the extension options into the packet.
//
for (Index = 0; Index < Token->OptionCount; ++Index) {
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].OptionStr);
- Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1);
- Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].ValueStr);
- Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;
- Len -= (UINT32)(AsciiStrLen ((CHAR8 *) Options[Index].ValueStr) + 1);
+ OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
+ ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (OptionStrLength + 1);
+ Cur += OptionStrLength + 1;
+
+ Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr);
+ ASSERT_EFI_ERROR (Status);
+ BufferLength -= (UINT32) (ValueStrLength + 1);
+ Cur += ValueStrLength + 1;
+
}

//
// Save the packet buf for retransmit
//
--
1.9.5.msysgit.1
Loading...