[edk2] [PATCH] MdePkg/BaseMemoryLib: Fix undefined behavior for left-shift operation

Hao Wu posted 1 patch 6 years ago
Failed in applying to current master (apply log)
MdePkg/Library/BaseMemoryLib/SetMem.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[edk2] [PATCH] MdePkg/BaseMemoryLib: Fix undefined behavior for left-shift operation
Posted by Hao Wu 6 years ago
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=783

Within function InternalMemSetMem(), for line:
  Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;

Expression '(Value << 24)' is possible to bring undefined behavior.
Since 'Value' is of type UINT8 (unsigned char), it will be implicitly
promoted to type 'int' before performing the left-shift operation.

It is possible for '(Value << 24)' to exceed the range of type 'int',
which may bring undefined behavior.

This commit add explicit type cast like:
((UINT32)Value << 24)

to resolve the issue.

Cc: Steven Shi <steven.shi@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
---
 MdePkg/Library/BaseMemoryLib/SetMem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/MdePkg/Library/BaseMemoryLib/SetMem.c b/MdePkg/Library/BaseMemoryLib/SetMem.c
index b6fb811c38..124b48fe5d 100644
--- a/MdePkg/Library/BaseMemoryLib/SetMem.c
+++ b/MdePkg/Library/BaseMemoryLib/SetMem.c
@@ -4,7 +4,7 @@
   build for a particular platform easily if an optimized version
   is desired.
 
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
   Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>
   Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
 
@@ -54,7 +54,7 @@ InternalMemSetMem (
 
   if ((((UINTN)Buffer & 0x7) == 0) && (Length >= 8)) {
     // Generate the 64bit value
-    Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;
+    Value32 = ((UINT32)Value << 24) | (Value << 16) | (Value << 8) | Value;
     Value64 = LShiftU64 (Value32, 32) | Value32;
 
     Pointer64 = (UINT64*)Buffer;
-- 
2.12.0.windows.1

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