[PATCH] nbd: fix uninitialized variable warning

pannengyuan@huawei.com posted 1 patch 5 years, 10 months ago
Test asan failed
Test checkpatch failed
Test FreeBSD failed
Test docker-mingw@fedora failed
Test docker-clang@ubuntu failed
Test docker-quick@centos7 failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20200106015443.38540-1-pannengyuan@huawei.com
Maintainers: Eric Blake <eblake@redhat.com>
There is a newer version of this series
nbd/server.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] nbd: fix uninitialized variable warning
Posted by pannengyuan@huawei.com 5 years, 10 months ago
From: Pan Nengyuan <pannengyuan@huawei.com>

Fixes:
/mnt/sdb/qemu/nbd/server.c: In function 'nbd_handle_request':
/mnt/sdb/qemu/nbd/server.c:2313:9: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     int ret;

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
---
 nbd/server.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nbd/server.c b/nbd/server.c
index 24ebc1a805..7eb3de0842 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -2310,7 +2310,7 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
                                            NBDRequest *request,
                                            uint8_t *data, Error **errp)
 {
-    int ret;
+    int ret = 0;
     int flags;
     NBDExport *exp = client->exp;
     char *msg;
-- 
2.21.0.windows.1



Re: [PATCH] nbd: fix uninitialized variable warning
Posted by Eric Blake 5 years, 10 months ago
On 1/5/20 7:54 PM, pannengyuan@huawei.com wrote:
> From: Pan Nengyuan <pannengyuan@huawei.com>
> 
> Fixes:
> /mnt/sdb/qemu/nbd/server.c: In function 'nbd_handle_request':
> /mnt/sdb/qemu/nbd/server.c:2313:9: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>       int ret;
> 
> Reported-by: Euler Robot <euler.robot@huawei.com>

False positive in the robot - I cannot see any path where ret is used 
uninitialized.  Closest might be the handling of NBD_CMD_BLOCK_STATUS, 
which looks like:

if (a || b) {
   if (a) {
     ret = ...;
     if (ret < 0) {
       return ret;
     }
   }
   if (b) {
     ret = ...;
     if (ret < 0) {
       return ret;
     }
   }
   return ret;
}

In fact, those 'if (ret < 0)' tests are pointless, since nothing else 
really happens before the final return ret.

If I'm right about this being what trips up the robot, does changing 'if 
(b)' into 'else' solve the problem, rather than adding an initializer? 
And if so, can we clean up the pointless code while at it?

> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
> ---
>   nbd/server.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/nbd/server.c b/nbd/server.c
> index 24ebc1a805..7eb3de0842 100644
> --- a/nbd/server.c
> +++ b/nbd/server.c
> @@ -2310,7 +2310,7 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
>                                              NBDRequest *request,
>                                              uint8_t *data, Error **errp)
>   {
> -    int ret;
> +    int ret = 0;
>       int flags;
>       NBDExport *exp = client->exp;
>       char *msg;
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


Re: [PATCH] nbd: fix uninitialized variable warning
Posted by Pan Nengyuan 5 years, 10 months ago

On 1/8/2020 6:24 AM, Eric Blake wrote:
> On 1/5/20 7:54 PM, pannengyuan@huawei.com wrote:
>> From: Pan Nengyuan <pannengyuan@huawei.com>
>>
>> Fixes:
>> /mnt/sdb/qemu/nbd/server.c: In function 'nbd_handle_request':
>> /mnt/sdb/qemu/nbd/server.c:2313:9: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>>       int ret;
>>
>> Reported-by: Euler Robot <euler.robot@huawei.com>
> 
> False positive in the robot - I cannot see any path where ret is used uninitialized.  Closest might be the handling of NBD_CMD_BLOCK_STATUS, which looks like:
> 
> if (a || b) {
>   if (a) {
>     ret = ...;
>     if (ret < 0) {
>       return ret;
>     }
>   }
>   if (b) {
>     ret = ...;
>     if (ret < 0) {
>       return ret;
>     }
>   }
>   return ret;
> }
> 
> In fact, those 'if (ret < 0)' tests are pointless, since nothing else really happens before the final return ret.
> 
> If I'm right about this being what trips up the robot, does changing 'if (b)' into 'else' solve the problem, rather than adding an initializer? And if so, can we clean up the pointless code while at it?

Yes, you are right, Changing 'if(b)' to 'else' solves the problem.
I will change it and clean up the pointless code in next version.

Thanks.

> 
>> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
>> ---
>>   nbd/server.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/nbd/server.c b/nbd/server.c
>> index 24ebc1a805..7eb3de0842 100644
>> --- a/nbd/server.c
>> +++ b/nbd/server.c
>> @@ -2310,7 +2310,7 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
>>                                              NBDRequest *request,
>>                                              uint8_t *data, Error **errp)
>>   {
>> -    int ret;
>> +    int ret = 0;
>>       int flags;
>>       NBDExport *exp = client->exp;
>>       char *msg;
>>
>