Skip to contentSkip to conversation

mmc_cmds.c: fix warning on uninitialized 'cnt'

PATCH Superseded Giulio Benetti 4 participants 10 messages Sep 18, 2022 – Sep 21, 2022 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc_cmds.c: fix warning on uninitialized 'cnt'

Since -Werror is enabled by default build fails with:

```
In function '__bswap_32',
    inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be used uninitialized [-Werror=maybe-uninitialized]
   52 |   return __builtin_bswap32 (__bsx);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
 2270 |         unsigned int cnt;
      |                      ^~~
cc1: all warnings being treated as errors
```

So let's fix the warning by initializing cnt local variable.

---
 mmc_cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +1 -1
index 12b7802..777d649 100644
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
 	int ret, dev_fd, key_fd, data_fd;
 	unsigned char key[32];
 	uint16_t addr;
-	unsigned int cnt;
+	unsigned int cnt = 0;
 	struct rpmb_frame frame_in = {
 		.req_resp    = htobe16(MMC_RPMB_WRITE),
 		.block_count = htobe16(1)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines

Since -Werror is enabled by default build fails with:
```
In function '__bswap_32',

    inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-
linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be
used uninitialized [-Werror=maybe-uninitialized]
   52 |   return __builtin_bswap32 (__bsx);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines

mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
2270 | unsigned int cnt;
| ^~~
cc1: all warnings being treated as errors ``` So let's fix the warning by initializing cnt local variable.

Please prefix you subject with "mmc-utils:"

Thanks,
Avri

Quoted 19 lines
---
 mmc_cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +1 -2
index 12b7802..777d649 100644
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
        int ret, dev_fd, key_fd, data_fd;
        unsigned char key[32];
        uint16_t addr;
-       unsigned int cnt;
+       unsigned int cnt = 0;
        struct rpmb_frame frame_in = {
                .req_resp    = htobe16(MMC_RPMB_WRITE),
                .block_count = htobe16(1)
--

2.34.1

#2 lore raw
Quoted 16 lines · Avri Altman
Quoted 16 lines

Since -Werror is enabled by default build fails with:
```
In function '__bswap_32',

     inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-
linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be
used uninitialized [-Werror=maybe-uninitialized]
    52 |   return __builtin_bswap32 (__bsx);
       |          ^~~~~~~~~~~~~~~~~~~~~~~~~

mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
2270 | unsigned int cnt;
| ^~~
cc1: all warnings being treated as errors ``` So let's fix the warning by initializing cnt local variable.

Quoted 2 lines
Quoted 1 line

Please prefix you subject with "mmc-utils:"

Ok, thank you. Going to send V2

Best regards

Signature · 2 lines
Giulio Benetti
Benetti Engineering sas
Quoted 23 lines

Thanks,
Avri

Quoted 19 lines
---
  mmc_cmds.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +1 -2
index 12b7802..777d649 100644
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
         int ret, dev_fd, key_fd, data_fd;
         unsigned char key[32];
         uint16_t addr;
-       unsigned int cnt;
+       unsigned int cnt = 0;
         struct rpmb_frame frame_in = {
                 .req_resp    = htobe16(MMC_RPMB_WRITE),
                 .block_count = htobe16(1)
--

2.34.1

#3 lore raw

PATCH v2 1/1 mmc-utils: mmc_cmds.c: fix warning on uninitialized 'cnt'

In reply to Avri Altman

---
V1->V2:
* prefix subjet with 'mmc-utils:' as pointed by Avri Altman

---
 mmc_cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +1 -1
index 12b7802..777d649 100644
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
 	int ret, dev_fd, key_fd, data_fd;
 	unsigned char key[32];
 	uint16_t addr;
-	unsigned int cnt;
+	unsigned int cnt = 0;
 	struct rpmb_frame frame_in = {
 		.req_resp    = htobe16(MMC_RPMB_WRITE),
 		.block_count = htobe16(1)
Signature · 1 line
2.34.1
#4 lore raw

RE: [PATCH v2] mmc-utils: mmc_cmds.c: fix warning on uninitialized 'cnt'

You forgot to include your commit log.
Also no need to include the module name - just mmc-utils: fix warning on uninitialized 'cnt'

Please feel free to add my reviewed-by once you do.

Thanks,
Avri

Quoted 24 lines

---
V1->V2:
* prefix subjet with 'mmc-utils:' as pointed by Avri Altman

---
 mmc_cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +1 -2
index 12b7802..777d649 100644
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
        int ret, dev_fd, key_fd, data_fd;
        unsigned char key[32];
        uint16_t addr;
-       unsigned int cnt;
+       unsigned int cnt = 0;
        struct rpmb_frame frame_in = {
                .req_resp    = htobe16(MMC_RPMB_WRITE),
                .block_count = htobe16(1)
--

2.34.1

#5 lore raw

Re: [PATCH v2] mmc-utils: mmc_cmds.c: fix warning on uninitialized 'cnt'

In reply to Giulio Benetti
Quoted 13 lines · Giulio Benetti

---
V1->V2:
* prefix subjet with 'mmc-utils:' as pointed by Avri Altman

---
 mmc_cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +0 -0
index 12b7802..777d649 100644

mmc_cmds.c int do_rpmb_write_block(int nargs, char **argv) · line 2442

Quoted 8 lines
mmc_cmds.c +1 -1
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
 	int ret, dev_fd, key_fd, data_fd;
 	unsigned char key[32];
 	uint16_t addr;
-	unsigned int cnt;
+	unsigned int cnt = 0;
 	struct rpmb_frame frame_in = {
 		.req_resp    = htobe16(MMC_RPMB_WRITE),
 		.block_count = htobe16(1)

I think this is the wrong place to initialize it, I would suggest doing it inside of rpmb_read_counter() when returning an error, to make it clear that this is not the intended counter but something that is not actually meant to be used.

If you initialize local variables to bogus values, you remove the possibility for the compiler to warn a function actually uses something without initialization.

Arnd

#6 lore raw

Re: [PATCH v2] mmc-utils: mmc_cmds.c: fix warning on uninitialized 'cnt'

Hi Arnd,

Quoted 28 lines · Arnd Bergmann
Quoted 13 lines · Giulio Benetti

---
V1->V2:
* prefix subjet with 'mmc-utils:' as pointed by Avri Altman

---
  mmc_cmds.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mmc_cmds.c +0 -0
index 12b7802..777d649 100644

mmc_cmds.c int do_rpmb_write_block(int nargs, char **argv) · line 2442

Quoted 8 lines
mmc_cmds.c +1 -1
@@ -2436,7 +2436,7 @@ int do_rpmb_write_block(int nargs, char **argv)
  	int ret, dev_fd, key_fd, data_fd;
  	unsigned char key[32];
  	uint16_t addr;
-	unsigned int cnt;
+	unsigned int cnt = 0;
  	struct rpmb_frame frame_in = {
  		.req_resp    = htobe16(MMC_RPMB_WRITE),
  		.block_count = htobe16(1)

I think this is the wrong place to initialize it, I would suggest doing it inside of rpmb_read_counter() when returning an error, to make it clear that this is not the intended counter but something that is not actually meant to be used.

Quoted 3 lines

If you initialize local variables to bogus values, you remove the possibility for the compiler to warn a function actually uses something without initialization.

Totally right, thank you.

Signature · 2 lines
Giulio Benetti
Benetti Engineering sas
#7 lore raw

PATCH v3 1/1 mmc-utils: fix warning on uninitialized 'cnt'

In reply to Arnd Bergmann

When building following warning shows up:

```
In function '__bswap_32',
    inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be used uninitialized [-Werror=maybe-uninitialized]
   52 |   return __builtin_bswap32 (__bsx);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
2270 |         unsigned int cnt;
      |                      ^~~
cc1: all warnings being treated as errors
```

This is due to function rpmb_read_counter() that doesn't set its argument 'unsigned int *cnt' in all return points. So let's set *cnt to 0 in the return point that misses to initialize it.

---
V1->V2:
* prefix subject with 'mmc-utils:' as pointed by Avri Altman V2->V3:
* add missing commit as pointed by Avri Altman
* initialize pointer inside rpmb_read_counter() as suggested by Arnd Bergmann

---
 mmc_cmds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mmc_cmds.c +3 -1
index 12b7802..4d203ef 100644
@@ -2238,8 +2238,10 @@ int rpmb_read_counter(int dev_fd, unsigned int *cnt)
 	}
 
 	/* Check RPMB response */
-	if (frame_out.result != 0)
+	if (frame_out.result != 0) {
+		*cnt = 0;
 		return be16toh(frame_out.result);
+	}
 
 	*cnt = be32toh(frame_out.write_counter);
 
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#8 lore raw

RE: [PATCH v3] mmc-utils: fix warning on uninitialized 'cnt'

Quoted 18 lines

When building following warning shows up:
```
In function '__bswap_32',

    inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-
linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be
used uninitialized [-Werror=maybe-uninitialized]
   52 |   return __builtin_bswap32 (__bsx);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~

mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
2270 | unsigned int cnt;
| ^~~
cc1: all warnings being treated as errors ``` This is due to function rpmb_read_counter() that doesn't set its argument 'unsigned int *cnt' in all return points. So let's set *cnt to 0 in the return point that misses to initialize it.

Quoted 1 line
Quoted 30 lines

---
V1->V2:
* prefix subject with 'mmc-utils:' as pointed by Avri Altman V2->V3:
* add missing commit as pointed by Avri Altman
* initialize pointer inside rpmb_read_counter() as suggested by Arnd Bergmann

---
 mmc_cmds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mmc_cmds.c +3 -2
index 12b7802..4d203ef 100644
@@ -2238,8 +2238,10 @@ int rpmb_read_counter(int dev_fd, unsigned int
 *cnt)
        }
 
        /* Check RPMB response */
-       if (frame_out.result != 0)
+       if (frame_out.result != 0) {
+               *cnt = 0;
                return be16toh(frame_out.result);
+       }
 
        *cnt = be32toh(frame_out.write_counter);
 
--

2.34.1

Applied
#9 lore raw

Re: [PATCH v3] mmc-utils: fix warning on uninitialized 'cnt'

In reply to Giulio Benetti
Quoted 14 lines · Giulio Benetti

When building following warning shows up:

```
In function '__bswap_32',
    inlined from 'do_rpmb_write_block' at mmc_cmds.c:2293:27:
/home/autobuild/autobuild/instance-15/output-1/host/aarch64-buildroot-linux-gnu/sysroot/usr/include/bits/byteswap.h:52:10: error: 'cnt' may be used uninitialized [-Werror=maybe-uninitialized]
   52 |   return __builtin_bswap32 (__bsx);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
mmc_cmds.c: In function 'do_rpmb_write_block':
mmc_cmds.c:2270:22: note: 'cnt' was declared here
2270 |         unsigned int cnt;
      |                      ^~~
cc1: all warnings being treated as errors
```
Quoted 5 lines

This is due to function rpmb_read_counter() that doesn't set its argument 'unsigned int *cnt' in all return points. So let's set *cnt to 0 in the return point that misses to initialize it.

Applied to git.kernel.org/pub/scm//utils/mmc/mmc-utils.git master, thanks!

Kind regards
Uffe

Quoted 29 lines

---
V1->V2:
* prefix subject with 'mmc-utils:' as pointed by Avri Altman V2->V3:
* add missing commit as pointed by Avri Altman
* initialize pointer inside rpmb_read_counter() as suggested by Arnd Bergmann

---
 mmc_cmds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mmc_cmds.c +3 -2
index 12b7802..4d203ef 100644
@@ -2238,8 +2238,10 @@ int rpmb_read_counter(int dev_fd, unsigned int *cnt)
        }
 
        /* Check RPMB response */
-       if (frame_out.result != 0)
+       if (frame_out.result != 0) {
+               *cnt = 0;
                return be16toh(frame_out.result);
+       }
 
        *cnt = be32toh(frame_out.write_counter);
 
--

2.34.1