Skip to contentSkip to conversation

migration: Fix parse_ramblock() on overwritten retvals

PATCH In review Peter Xu 4 participants 7 messages Oct 17, 2023 – Oct 19, 2023

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 migration: Fix parse_ramblock() on overwritten retvals

It's possible that some errors can be overwritten with success retval later on, and then ignored. Always capture all errors and report.

Reported by Coverity 1522861, but actually I spot one more in the same function.

Fixes CID 1522861
---
 migration/ram.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
migration/ram.c +4 -2
index c844151ee9..d8bdb53a8f 100644
@@ -3888,6 +3888,8 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
         ret = qemu_ram_resize(block, length, &local_err);
         if (local_err) {
             error_report_err(local_err);
+            assert(ret < 0);
+            return ret;
         }
     }
     /* For postcopy we need to check hugepage sizes match */
@@ -3898,7 +3900,7 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
             error_report("Mismatched RAM page size %s "
                          "(local) %zd != %" PRId64, block->idstr,
                          block->page_size, remote_page_size);
-            ret = -EINVAL;
+            return -EINVAL;
         }
     }
     if (migrate_ignore_shared()) {
@@ -3908,7 +3910,7 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
             error_report("Mismatched GPAs for block %s "
                          "%" PRId64 "!= %" PRId64, block->idstr,
                          (uint64_t)addr, (uint64_t)block->mr->addr);
-            ret = -EINVAL;
+            return -EINVAL;
         }
     }
     ret = rdma_block_notification_handle(f, block->idstr);
Signature · 1 line
2.41.0
#2 lore raw
In reply to Peter Xu
Quoted 17 lines · Peter Xu

It's possible that some errors can be overwritten with success retval later on, and then ignored. Always capture all errors and report.

Reported by Coverity 1522861, but actually I spot one more in the same function.

Fixes CID 1522861
---
 migration/ram.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
migration/ram.c +0 -0
index c844151ee9..d8bdb53a8f 100644

migration/ram.c static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) · line 3892

Quoted 5 lines
migration/ram.c +2 -0
@@ -3888,6 +3888,8 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
         ret = qemu_ram_resize(block, length, &local_err);
         if (local_err) {
             error_report_err(local_err);
+            assert(ret < 0);
+            return ret;

I hate that assert. If you really want that:

         if (ret < 0) {
            error_report_err(local_err);
            assert(ret < 0);
            return ret;
         }

Rest of the patch looks ok.

Later, Juan.

author
#3 lore raw
Quoted 13 lines · Juan Quintela
Quoted 12 lines · Peter Xu

It's possible that some errors can be overwritten with success retval later on, and then ignored. Always capture all errors and report.

Reported by Coverity 1522861, but actually I spot one more in the same function.

Fixes CID 1522861
---
 migration/ram.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 12 lines

migration/ram.c static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) · line 3892

Quoted 10 lines
migration/ram.c +2 -0
index c844151ee9..d8bdb53a8f 100644
@@ -3888,6 +3888,8 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
         ret = qemu_ram_resize(block, length, &local_err);
         if (local_err) {
             error_report_err(local_err);
+            assert(ret < 0);
+            return ret;

I hate that assert. If you really want that:

Please have a look at qemu_ram_resize(). It only contains two error paths.

Quoted 4 lines

         if (ret < 0) {
            error_report_err(local_err);

This will be similar to above, if qemu_ram_resize() return <0 with err==NULL, it'll crash in error_report_err() too.. at error_get_pretty().

Quoted 1 line
            assert(ret < 0);

This is not necessary.. if in this "if" section. So we can drop it (instead of assert it).

Quoted 4 lines
            return ret;
         }

Rest of the patch looks ok.

I tend to prefer just merging this.. but if you strongly prefer the other way, I can drop the assert(). But then I'll prefer "return -EINVAL" rather than "return ret", if you're fine with it.

Thanks,

Signature · 1 line
Peter Xu
#5 lore raw
In reply to Peter Xu
Quoted 6 lines · Peter Xu

It's possible that some errors can be overwritten with success retval later on, and then ignored. Always capture all errors and report.

Reported by Coverity 1522861, but actually I spot one more in the same function.

The other one is CID 1522862, I think.

Quoted 12 lines
Fixes CID 1522861
---
 migration/ram.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
migration/ram.c +0 -0
index c844151ee9..d8bdb53a8f 100644

migration/ram.c static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) · line 3891

Quoted 4 lines
migration/ram.c +1 -0
@@ -3888,6 +3888,8 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
         ret = qemu_ram_resize(block, length, &local_err);
         if (local_err) {
             error_report_err(local_err);
+            assert(ret < 0);

We usually don't bother asserting for this kind of "function reports errors two ways" code.

migration/ram.c static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) · line 3893

Quoted 2 lines
migration/ram.c +1 -0
+            return ret;
         }

thanks
-- PMM

author
#6 lore raw
Quoted 9 lines · Peter Maydell
Quoted 6 lines · Peter Xu

It's possible that some errors can be overwritten with success retval later on, and then ignored. Always capture all errors and report.

Reported by Coverity 1522861, but actually I spot one more in the same function.

The other one is CID 1522862, I think.

Yes..

Quoted 8 lines
Quoted 8 lines
Fixes CID 1522861
---
 migration/ram.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 12 lines

migration/ram.c static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) · line 3891

Quoted 9 lines
migration/ram.c +1 -0
index c844151ee9..d8bdb53a8f 100644
@@ -3888,6 +3888,8 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
         ret = qemu_ram_resize(block, length, &local_err);
         if (local_err) {
             error_report_err(local_err);
+            assert(ret < 0);

We usually don't bother asserting for this kind of "function reports errors two ways" code.

Juan, please feel free to drop the assert() if it's in the queue.

After this one lands, I'll send a patch to remove qemu_ram_resize retval and only rely on Error*.

Thanks,

Signature · 1 line
Peter Xu