[edk2,v2] MdePkg/BaseMemoryLibOptDxe: check for zero length in ZeroMem ()

Message ID 1478197093-17209-1-git-send-email-ard.biesheuvel@linaro.org
State New
Headers show

Commit Message

Ard Biesheuvel Nov. 3, 2016, 6:18 p.m.
Unlike other string functions in this library, ZeroMem () does not
return early when the length of the input buffer is 0. So add the
same to ZeroMem () as well.

This fixes an issue with the ARM implementation, whose InternalMemZeroMem
code does not expect a length of 0, and always writes at least a single
byte.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Comments

Laszlo Ersek Nov. 3, 2016, 6:31 p.m. | #1
On 11/03/16 19:18, Ard Biesheuvel wrote:
> Unlike other string functions in this library, ZeroMem () does not

> return early when the length of the input buffer is 0. So add the

> same to ZeroMem () as well.

> 

> This fixes an issue with the ARM implementation, whose InternalMemZeroMem

> code does not expect a length of 0, and always writes at least a single

> byte.

> 

> Contributed-under: TianoCore Contribution Agreement 1.0

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

> ---

>  MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c | 6 +++++-

>  1 file changed, 5 insertions(+), 1 deletion(-)

> 

> diff --git a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> index 2a0a038fd6c5..9dd0b45e188e 100644

> --- a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> +++ b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> @@ -46,7 +46,11 @@ ZeroMem (

>    IN UINTN  Length

>    )

>  {

> -  ASSERT (!(Buffer == NULL && Length > 0));

> +  if (Length == 0) {

> +    return Buffer;

> +  }

> +

> +  ASSERT (Buffer != NULL);

>    ASSERT (Length <= (MAX_ADDRESS - (UINTN)Buffer + 1));

>    return InternalMemZeroMem (Buffer, Length);

>  }

> 


Assuming the ARM impl of InternalMemZeroMem() conforms to the
InternalMemZeroMem() contract -- if there is such a contract --, I think
this patch makes sense:

Acked-by: Laszlo Ersek <lersek@redhat.com>


Thanks
Laszlo
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Gao, Liming Nov. 4, 2016, 2:05 a.m. | #2
Ard:
  Could you help apply this changes for other BaseMemoryLib instances in MdePkg? 

Thanks
Liming
> -----Original Message-----

> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]

> Sent: Friday, November 04, 2016 2:18 AM

> To: edk2-devel@lists.01.org; Kinney, Michael D

> <michael.d.kinney@intel.com>; Gao, Liming <liming.gao@intel.com>

> Cc: lersek@redhat.com; Carsey, Jaben <jaben.carsey@intel.com>; Ard

> Biesheuvel <ard.biesheuvel@linaro.org>

> Subject: [PATCH v2] MdePkg/BaseMemoryLibOptDxe: check for zero length

> in ZeroMem ()

> 

> Unlike other string functions in this library, ZeroMem () does not

> return early when the length of the input buffer is 0. So add the

> same to ZeroMem () as well.

> 

> This fixes an issue with the ARM implementation, whose

> InternalMemZeroMem

> code does not expect a length of 0, and always writes at least a single

> byte.

> 

> Contributed-under: TianoCore Contribution Agreement 1.0

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

> ---

>  MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c | 6 +++++-

>  1 file changed, 5 insertions(+), 1 deletion(-)

> 

> diff --git a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> index 2a0a038fd6c5..9dd0b45e188e 100644

> --- a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> +++ b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c

> @@ -46,7 +46,11 @@ ZeroMem (

>    IN UINTN  Length

>    )

>  {

> -  ASSERT (!(Buffer == NULL && Length > 0));

> +  if (Length == 0) {

> +    return Buffer;

> +  }

> +

> +  ASSERT (Buffer != NULL);

>    ASSERT (Length <= (MAX_ADDRESS - (UINTN)Buffer + 1));

>    return InternalMemZeroMem (Buffer, Length);

>  }

> --

> 2.7.4


_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Patch

diff --git a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c
index 2a0a038fd6c5..9dd0b45e188e 100644
--- a/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c
+++ b/MdePkg/Library/BaseMemoryLibOptDxe/ZeroMemWrapper.c
@@ -46,7 +46,11 @@  ZeroMem (
   IN UINTN  Length
   )
 {
-  ASSERT (!(Buffer == NULL && Length > 0));
+  if (Length == 0) {
+    return Buffer;
+  }
+
+  ASSERT (Buffer != NULL);
   ASSERT (Length <= (MAX_ADDRESS - (UINTN)Buffer + 1));
   return InternalMemZeroMem (Buffer, Length);
 }