public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] MdePkg/BaseMemoryLib: Fix undefined behavior for left-shift operation
@ 2018-03-22  3:10 Hao Wu
  0 siblings, 0 replies; only message in thread
From: Hao Wu @ 2018-03-22  3:10 UTC (permalink / raw)
  To: edk2-devel; +Cc: Hao Wu, Steven Shi, Michael D Kinney, Liming Gao

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



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2018-03-22  3:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-22  3:10 [PATCH] MdePkg/BaseMemoryLib: Fix undefined behavior for left-shift operation Hao Wu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox