Skip to contentSkip to conversation

soc: mediatek: pwrap: Zero initialize rdata in pwrap_init_cipher

PATCH In review Nathan Chancellor 4 participants 5 messages Mar 7, 2019 – Apr 12, 2019 Arm MediaTek

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 soc: mediatek: pwrap: Zero initialize rdata in pwrap_init_cipher

When building with -Wsometimes-uninitialized, Clang warns:

drivers/soc/mediatek/mtk-pmic-wrap.c:1358:6: error: variable 'rdata' is used uninitialized whenever '||' condition is true [-Werror,-Wsometimes-uninitialized]

If pwrap_write returns non-zero, pwrap_read will not be called to initialize rdata, meaning that we will use some random uninitialized stack value in our print statement. Zero initialize rdata in case this happens.

---

I don't know if this is better or to just restructure the if statement below (I'm not an expert in this code so I'll leave that up to the maintainers to decide).

 drivers/soc/mediatek/mtk-pmic-wrap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/mediatek/mtk-pmic-wrap.c +1 -1
index 8236a6c87e19..2f632e8790f7 100644
@@ -1281,7 +1281,7 @@ static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
 static int pwrap_init_cipher(struct pmic_wrapper *wrp)
 {
 	int ret;
-	u32 rdata;
+	u32 rdata = 0;
 
 	pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
 	pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/soc/mediatek/mtk-pmic-wrap.c:1358:6: error: variable 'rdata' is used uninitialized whenever '||' condition is true [-Werror,-Wsometimes-uninitialized]

If pwrap_write returns non-zero, pwrap_read will not be called to initialize rdata, meaning that we will use some random uninitialized stack value in our print statement. Zero initialize rdata in case this happens.

Quoted 7 lines

---

I don't know if this is better or to just restructure the if statement below (I'm not an expert in this code so I'll leave that up to the maintainers to decide).

No, I the way you have it here is most correct. That condition writes a value somewhere, reads it back, then compares it. The write or read could fail. Better to just initialize rdata in case the write fails. Thanks for the patch.

Quoted 20 lines
 drivers/soc/mediatek/mtk-pmic-wrap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/mediatek/mtk-pmic-wrap.c +1 -2
index 8236a6c87e19..2f632e8790f7 100644
@@ -1281,7 +1281,7 @@ static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
 static int pwrap_init_cipher(struct pmic_wrapper *wrp)
 {
        int ret;
-       u32 rdata;
+       u32 rdata = 0;
 
        pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
        pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
--

2.21.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
Reviewed-by
#2 lore raw
Quoted 23 lines · Nick Desaulniers' via Clang Built Linux
Quoted 12 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/soc/mediatek/mtk-pmic-wrap.c:1358:6: error: variable 'rdata' is used uninitialized whenever '||' condition is true [-Werror,-Wsometimes-uninitialized]

If pwrap_write returns non-zero, pwrap_read will not be called to initialize rdata, meaning that we will use some random uninitialized stack value in our print statement. Zero initialize rdata in case this happens.

Quoted 7 lines

---

I don't know if this is better or to just restructure the if statement below (I'm not an expert in this code so I'll leave that up to the maintainers to decide).

Quoted 5 lines

No, I the way you have it here is most correct. That condition writes a value somewhere, reads it back, then compares it. The write or read could fail. Better to just initialize rdata in case the write fails. Thanks for the patch.

I also came up with a similar patch here, but I move the initialization to just before the first use of the variable:

(quoted diff) +0 -0

b/drivers/soc/mediatek/mtk-pmic-wrap.c
index 8236a6c87e19..eb5035fd8ecd 100644

drivers/soc/mediatek/mtk-pmic-wrap.c +1 -0
@@ -1355,6 +1355,7 @@ static int pwrap_init_cipher(struct pmic_wrapper *wrp)
        pwrap_writel(wrp, 1, PWRAP_CIPHER_MODE);
 
        /* Write Test */
+       rdata = 0;
        if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_WRITE_TEST],
                        PWRAP_DEW_WRITE_TEST_VAL) ||
            pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_WRITE_TEST],

Very little difference, so let's go with your patch

#3 lore raw
In reply to Nathan Chancellor
Quoted 38 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/soc/mediatek/mtk-pmic-wrap.c:1358:6: error: variable 'rdata' is used uninitialized whenever '||' condition is true [-Werror,-Wsometimes-uninitialized]

If pwrap_write returns non-zero, pwrap_read will not be called to initialize rdata, meaning that we will use some random uninitialized stack value in our print statement. Zero initialize rdata in case this happens.

---

I don't know if this is better or to just restructure the if statement below (I'm not an expert in this code so I'll leave that up to the maintainers to decide).

 drivers/soc/mediatek/mtk-pmic-wrap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/mediatek/mtk-pmic-wrap.c +1 -1
index 8236a6c87e19..2f632e8790f7 100644
@@ -1281,7 +1281,7 @@ static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
 static int pwrap_init_cipher(struct pmic_wrapper *wrp)
 {
 	int ret;
-	u32 rdata;
+	u32 rdata = 0;
 
 	pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
 	pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
Signature · 1 line
2.21.0

Gentle ping (if there was a response to this, I didn't receive it). I know I sent it in the middle of a merge window so I get if it slipped through the cracks.

Thanks,
Nathan

maintainer
#4 lore raw
Quoted 34 lines · Nathan Chancellor
Quoted 33 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/soc/mediatek/mtk-pmic-wrap.c:1358:6: error: variable 'rdata' is used uninitialized whenever '||' condition is true [-Werror,-Wsometimes-uninitialized]

If pwrap_write returns non-zero, pwrap_read will not be called to initialize rdata, meaning that we will use some random uninitialized stack value in our print statement. Zero initialize rdata in case this happens.

---

I don't know if this is better or to just restructure the if statement below (I'm not an expert in this code so I'll leave that up to the maintainers to decide).

 drivers/soc/mediatek/mtk-pmic-wrap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/mediatek/mtk-pmic-wrap.c +1 -1
index 8236a6c87e19..2f632e8790f7 100644
@@ -1281,7 +1281,7 @@ static bool pwrap_is_pmic_cipher_ready(struct pmic_wrapper *wrp)
 static int pwrap_init_cipher(struct pmic_wrapper *wrp)
 {
 	int ret;
-	u32 rdata;
+	u32 rdata = 0;
 
Quoted 10 lines
Quoted 5 lines
drivers/soc/mediatek/mtk-pmic-wrap.c +0 -0
 	pwrap_writel(wrp, 0x1, PWRAP_CIPHER_SWRST);
 	pwrap_writel(wrp, 0x0, PWRAP_CIPHER_SWRST);
Signature · 1 line
2.21.0

Gentle ping (if there was a response to this, I didn't receive it). I know I sent it in the middle of a merge window so I get if it slipped through the cracks.

applied now to v5.1-next/soc

thanks