Skip to contentSkip to conversation

migration/ram: Fix compilation with -Wshadow=local

PATCH Superseded Thomas Huth 3 participants 9 messages Oct 23, 2023 – Oct 24, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration/ram: Fix compilation with -Wshadow=local

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
 migration/ram.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +1 -2
index 92769902bb..9de9e54fa9 100644
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
         ram_flush_compressed_data(rs);
 
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {
             qemu_file_set_error(f, ret);
         }
     }
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
 migration/ram.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3244

Quoted 9 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
         ram_flush_compressed_data(rs);
 
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {
             qemu_file_set_error(f, ret);
         }
     }

And queued. Thanks!

#2 lore raw
Quoted 28 lines · Markus Armbruster
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
 migration/ram.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3244

Quoted 9 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
         ram_flush_compressed_data(rs);
 
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {
             qemu_file_set_error(f, ret);
         }
     }
Quoted 1 line

And queued. Thanks!

Unqueued.

#3 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
 migration/ram.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3241

Quoted 6 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
         ram_flush_compressed_data(rs);
 
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {

We may need to rename "ret" to something else? qemu_file_set_error(), right below, will reference the error returned.

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3242

Quoted 1 line
migration/ram.c +0 -0
             qemu_file_set_error(f, ret);   <-----------------

Thanks,

Quoted 5 lines
migration/ram.c +0 -0
         }
     }
Signature · 1 line
2.41.0
Signature · 1 line
Peter Xu
#4 lore raw
Quoted 16 lines · Peter Xu
Quoted 15 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
  migration/ram.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
Quoted 10 lines

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3241

Quoted 5 lines
migration/ram.c +1 -2
          ram_flush_compressed_data(rs);
 
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {

We may need to rename "ret" to something else? qemu_file_set_error(), right below, will reference the error returned.

Quoted 1 line
migration/ram.c +0 -0
              qemu_file_set_error(f, ret);   <-----------------

Oh, drat, right ... that's exactly one of the reasons why shadowing variables is a bad idea ;-)

I'll redo a v2.

Thomas

#5 lore raw

ram_save_complete() is fishy (was: Re: [PATCH] migration/ram: Fix compilation with -Wshadow=local)

Quoted 25 lines · Thomas Huth
Quoted 24 lines · Peter Xu
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
  migration/ram.c | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, void · line 3240

Quoted 5 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f, void 
 *opaque)
           ram_flush_compressed_data(rs);
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {

We may need to rename "ret" to something else?  qemu_file_set_error(), right below, will reference the error returned.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

              qemu_file_set_error(f, ret);   <-----------------

Oh, drat, right ... that's exactly one of the reasons why shadowing variables is a bad idea ;-)

I'll redo a v2.

Actually, there is more fishy stuff in this function:

static int ram_save_complete(QEMUFile *f, void *opaque)

{
    ...
    int ret = 0;
    ...
    WITH_RCU_READ_LOCK_GUARD() {
        ...
        ret = rdma_registration_start(f, RAM_CONTROL_FINISH);
        if (ret < 0) {
            qemu_file_set_error(f, ret);
### here we use the outer "ret" variable         ###
        }
        ...
        while (true) {
            int pages;

            pages = ram_find_and_save_block(rs);
            /* no more blocks to sent */
            if (pages == 0) {
### here we break without touching "ret" (preserving the previous error) ###
                break;
            }
            if (pages < 0) {
                ret = pages;
###  we only replace the outer "ret" in this break-case here
                break;
            }
        }
        ...
        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
### so while ret from rdma_registration_start() might be propageted
### below, the ret from rdma_registration_stop() is only local here?
        if (ret < 0) {
            qemu_file_set_error(f, ret);
        }
    }

    if (ret < 0) {
### this might trigger by the "ret" from rdma_registration_start() but
### not by the one from rdma_registration_stop()? ... very weird...
        return ret;
    }

Looks like commit 48408174a7ec7 messed up with the return types pretty badly ... any suggestions what's the right way forward here? Should the return value of rdma_registration_start() only be used for the qemu_file_set_error(), too? Or should the return value of rdma_registration_stop() be allowed to be used for the "return ret" at the end, too?

Thomas

#6 lore raw

Re: ram_save_complete() is fishy (was: Re: [PATCH] migration/ram: Fix compilation with -Wshadow=local)

Quoted 45 lines · Thomas Huth
Quoted 25 lines · Thomas Huth
Quoted 24 lines · Peter Xu
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
  migration/ram.c | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, · line 3240

Quoted 5 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f,
 void *opaque)
           ram_flush_compressed_data(rs);
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {

We may need to rename "ret" to something else?  qemu_file_set_error(), right below, will reference the error returned.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

              qemu_file_set_error(f, ret);   <-----------------

Oh, drat, right ... that's exactly one of the reasons why shadowing variables is a bad idea ;-)

I'll redo a v2.

Actually, there is more fishy stuff in this function:

static int ram_save_complete(QEMUFile *f, void *opaque)

{
    ...
    int ret = 0;
    ...
    WITH_RCU_READ_LOCK_GUARD() {
        ...
        ret = rdma_registration_start(f, RAM_CONTROL_FINISH);
        if (ret < 0) {
            qemu_file_set_error(f, ret);
Quoted 1 line

### here we use the outer "ret" variable ###

[1]

Quoted 15 lines
        }
        ...
        while (true) {
            int pages;

            pages = ram_find_and_save_block(rs);
            /* no more blocks to sent */
            if (pages == 0) {
### here we break without touching "ret" (preserving the previous error) ###
                break;
            }
            if (pages < 0) {
                ret = pages;
###  we only replace the outer "ret" in this break-case here
                break;
Quoted 8 lines
            }
        }
        ...
        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
### so while ret from rdma_registration_start() might be propageted
### below, the ret from rdma_registration_stop() is only local here?
        if (ret < 0) {
            qemu_file_set_error(f, ret);

[2]

Quoted 9 lines
        }
    }

    if (ret < 0) {
### this might trigger by the "ret" from rdma_registration_start() but
### not by the one from rdma_registration_stop()? ... very weird...
        return ret;
    }
Quoted 6 lines

Looks like commit 48408174a7ec7 messed up with the return types pretty badly ... any suggestions what's the right way forward here? Should the return value of rdma_registration_start() only be used for the qemu_file_set_error(), too? Or should the return value of rdma_registration_stop() be allowed to be used for the "return ret" at the end, too?

Right that's indeed confusing, but it seems confusing too even before that commit. AFAICT, we should "break" for both [1][2] above for any error occured..

Thanks,

Signature · 1 line
Peter Xu
#7 lore raw

Re: ram_save_complete() is fishy

Quoted 92 lines · Peter Xu
Quoted 45 lines · Thomas Huth
Quoted 25 lines · Thomas Huth
Quoted 24 lines · Peter Xu
Quoted 14 lines · Thomas Huth

No need for a new variable here, especially not for one that shadows a variable from the beginning of the function scope. With this change the code now successfully compiles with -Wshadow=local.

---
   migration/ram.c | 3 +--
   1 file changed, 1 insertion(+), 2 deletions(-)
migration/ram.c +0 -0
index 92769902bb..9de9e54fa9 100644

migration/ram.c static int ram_save_complete(QEMUFile *f, · line 3240

Quoted 5 lines
migration/ram.c +1 -2
@@ -3238,8 +3238,7 @@ static int ram_save_complete(QEMUFile *f,
 void *opaque)
           ram_flush_compressed_data(rs);
-        int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
-        if (ret < 0) {
+        if (rdma_registration_stop(f, RAM_CONTROL_FINISH) < 0) {

We may need to rename "ret" to something else?  qemu_file_set_error(), right below, will reference the error returned.

Quoted 6 lines
Quoted 1 line

migration/ram.c static int ram_save_complete(QEMUFile *f, · line 3241

Quoted 1 line
migration/ram.c +0 -0
               qemu_file_set_error(f, ret);   <-----------------

Oh, drat, right ... that's exactly one of the reasons why shadowing variables is a bad idea ;-)

I'll redo a v2.

Actually, there is more fishy stuff in this function:

static int ram_save_complete(QEMUFile *f, void *opaque)

{
     ...
     int ret = 0;
     ...
     WITH_RCU_READ_LOCK_GUARD() {
         ...
         ret = rdma_registration_start(f, RAM_CONTROL_FINISH);
         if (ret < 0) {
             qemu_file_set_error(f, ret);
Quoted 1 line

### here we use the outer "ret" variable ###

[1]

Quoted 15 lines
         }
         ...
         while (true) {
             int pages;

             pages = ram_find_and_save_block(rs);
             /* no more blocks to sent */
             if (pages == 0) {
### here we break without touching "ret" (preserving the previous error) ###
                 break;
             }
             if (pages < 0) {
                 ret = pages;
###  we only replace the outer "ret" in this break-case here
                 break;
Quoted 8 lines
             }
         }
         ...
         int ret = rdma_registration_stop(f, RAM_CONTROL_FINISH);
### so while ret from rdma_registration_start() might be propageted
### below, the ret from rdma_registration_stop() is only local here?
         if (ret < 0) {
             qemu_file_set_error(f, ret);

[2]

Quoted 9 lines
         }
     }

     if (ret < 0) {
### this might trigger by the "ret" from rdma_registration_start() but
### not by the one from rdma_registration_stop()? ... very weird...
         return ret;
     }
Quoted 6 lines

Looks like commit 48408174a7ec7 messed up with the return types pretty badly ... any suggestions what's the right way forward here? Should the return value of rdma_registration_start() only be used for the qemu_file_set_error(), too? Or should the return value of rdma_registration_stop() be allowed to be used for the "return ret" at the end, too?

Quoted 3 lines

Right that's indeed confusing, but it seems confusing too even before that commit. AFAICT, we should "break" for both [1][2] above for any error occured..

Oh well, looking at the whole file, it seems like most spots that call a rdma_* function just do qemu_file_set_error() afterwards, but then continue with the normal workflow... that looks really confusing to me - if this needs fixing, it should be done by somebody who knows that code better than me, so I'll keep my hands of this and let somebody else fix it if necessary. I'll just respin my original patch to fix the -Wshadow issue.

Thomas

#8 lore raw

Re: ram_save_complete() is fishy

Quoted 6 lines · Thomas Huth

Oh well, looking at the whole file, it seems like most spots that call a rdma_* function just do qemu_file_set_error() afterwards, but then continue with the normal workflow... that looks really confusing to me - if this needs fixing, it should be done by somebody who knows that code better than me, so I'll keep my hands of this and let somebody else fix it if necessary. I'll just respin my original patch to fix the -Wshadow issue.

Oh.. let me take a closer look today. Thanks for helping, Thomas!

Signature · 1 line
Peter Xu