public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Hao Wu <hao.a.wu@intel.com>
To: edk2-devel@lists.01.org
Cc: Hao Wu <hao.a.wu@intel.com>, Steven Shi <steven.shi@intel.com>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Liming Gao <liming.gao@intel.com>
Subject: [PATCH] MdePkg/BaseMemoryLib: Fix undefined behavior for left-shift operation
Date: Thu, 22 Mar 2018 11:10:21 +0800	[thread overview]
Message-ID: <20180322031021.10796-1-hao.a.wu@intel.com> (raw)

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



                 reply	other threads:[~2018-03-22  3:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180322031021.10796-1-hao.a.wu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox