public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Haibo Chen <haibo.chen@nxp.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] mmc: cat u8 to u64 to avoid unexpected error
Date: Tue, 13 Sep 2016 16:27:57 +0800	[thread overview]
Message-ID: <1473755277-23489-1-git-send-email-haibo.chen@nxp.com> (raw)

Suspicious implicit sign extension exist. ext_csd[] is defined
as "u8", capacity is defined as u64, so u8 is promoted to signed
int first int the "|" expersion, then the sign extended to u64.
if the tmp sign value is largeer than 0x7fffffff, after the sign
extension, the upper bits of the result will all be 1.
Thanks to coverity <http://www.coverity.com>

e.g.
	u8  data_8;
	u64 data_64;

	data_8 = 0x80;
	data_64 = data_8 << 24; //0xffffffff80000000
	data_64 = ((u64)data_8) << 24;  //0x80000000

Signed-off-by: Haibo Chen <haibo.chen@nxp.com>
---
 drivers/mmc/mmc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index 43ea0bb..c1d1dc6 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -1176,10 +1176,10 @@ static int mmc_startup(struct mmc *mmc)
 			 * ext_csd's capacity is valid if the value is more
 			 * than 2GB
 			 */
-			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
-					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
-					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
-					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
+			capacity = ((u64)ext_csd[EXT_CSD_SEC_CNT]) << 0
+					| ((u64)ext_csd[EXT_CSD_SEC_CNT + 1]) << 8
+					| ((u64)ext_csd[EXT_CSD_SEC_CNT + 2]) << 16
+					| ((u64)ext_csd[EXT_CSD_SEC_CNT + 3]) << 24;
 			capacity *= MMC_MAX_BLOCK_LEN;
 			if ((capacity >> 20) > 2 * 1024)
 				mmc->capacity_user = capacity;
-- 
1.9.1

             reply	other threads:[~2016-09-13  8:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-13  8:27 Haibo Chen [this message]
2016-09-18 17:53 ` [U-Boot] [PATCH] mmc: cat u8 to u64 to avoid unexpected error Tom Rini
2016-09-19  6:31   ` Jaehoon Chung
2016-09-19 11:30     ` Tom Rini
2016-09-20  2:04       ` Jaehoon Chung
2016-09-20  2:12         ` Tom Rini

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-all 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=1473755277-23489-1-git-send-email-haibo.chen@nxp.com \
    --to=haibo.chen@nxp.com \
    --cc=u-boot@lists.denx.de \
    /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