If we want to add some info to errp (by error_prepend() or
error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro.
Otherwise, this info will not be added when errp == &fatal_err
(the program will exit prior to the error_append_hint() or
error_prepend() call).  Fix such cases.

If we want to check error after errp-function call, we need to
introduce local_err and than propagate it to errp. Instead, use
ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or
   &error_fatel, this means that we don't break error_abort
   (we'll abort on error_set, not on error_propagate)

This commit (together with its neighbors) was generated by

for f in $(git grep -l errp \*.[ch]); do \
    spatch --sp-file scripts/coccinelle/auto-propagated-errp.cocci \
    --macro-file scripts/cocci-macro-file.h --in-place --no-show-diff $f; \
done;

then fix a bit of compilation problems: coccinelle for some reason
leaves several
f() {
    ...
    goto out;
    ...
    out:
}
patterns, with "out:" at function end.

then
./python/commit-per-subsystem.py MAINTAINERS "$(< auto-msg)"

(auto-msg was a file with this commit message)

Still, for backporting it may be more comfortable to use only the first
command and then do one huge commit.

Reported-by: Kevin Wolf <kw...@redhat.com>
Reported-by: Greg Kurz <gr...@kaod.org>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@virtuozzo.com>
---
 backends/hostmem-file.c  | 21 ++++++++------------
 backends/hostmem-memfd.c | 18 ++++++++----------
 backends/hostmem.c       | 41 ++++++++++++++++++----------------------
 3 files changed, 34 insertions(+), 46 deletions(-)

diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index be64020746..4d03b9f33a 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -116,25 +116,22 @@ static void file_memory_backend_set_align(Object *o, 
Visitor *v,
                                           const char *name, void *opaque,
                                           Error **errp)
 {
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackend *backend = MEMORY_BACKEND(o);
     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
-    Error *local_err = NULL;
     uint64_t val;
 
     if (host_memory_backend_mr_inited(backend)) {
-        error_setg(&local_err, "cannot change property '%s' of %s",
+        error_setg(errp, "cannot change property '%s' of %s",
                    name, object_get_typename(o));
-        goto out;
+        return;
     }
 
-    visit_type_size(v, name, &val, &local_err);
-    if (local_err) {
-        goto out;
+    visit_type_size(v, name, &val, errp);
+    if (*errp) {
+        return;
     }
     fb->align = val;
-
- out:
-    error_propagate(errp, local_err);
 }
 
 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
@@ -144,6 +141,7 @@ static bool file_memory_backend_get_pmem(Object *o, Error 
**errp)
 
 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
 {
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackend *backend = MEMORY_BACKEND(o);
     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 
@@ -156,13 +154,10 @@ static void file_memory_backend_set_pmem(Object *o, bool 
value, Error **errp)
 
 #ifndef CONFIG_LIBPMEM
     if (value) {
-        Error *local_err = NULL;
-
-        error_setg(&local_err,
+        error_setg(errp,
                    "Lack of libpmem support while setting the 'pmem=on'"
                    " of %s. We can't ensure data persistence.",
                    object_get_typename(o));
-        error_propagate(errp, local_err);
         return;
     }
 #endif
diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
index 26070b425e..1f0677e5b4 100644
--- a/backends/hostmem-memfd.c
+++ b/backends/hostmem-memfd.c
@@ -77,27 +77,25 @@ static void
 memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name,
                               void *opaque, Error **errp)
 {
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
-    Error *local_err = NULL;
     uint64_t value;
 
     if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) {
-        error_setg(&local_err, "cannot change property value");
-        goto out;
+        error_setg(errp, "cannot change property value");
+        return;
     }
 
-    visit_type_size(v, name, &value, &local_err);
-    if (local_err) {
-        goto out;
+    visit_type_size(v, name, &value, errp);
+    if (*errp) {
+        return;
     }
     if (!value) {
-        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+        error_setg(errp, "Property '%s.%s' doesn't take value '%"
                    PRIu64 "'", object_get_typename(obj), name, value);
-        goto out;
+        return;
     }
     m->hugetlbsize = value;
-out:
-    error_propagate(errp, local_err);
 }
 
 static void
diff --git a/backends/hostmem.c b/backends/hostmem.c
index 6d333dc23c..7a3dd9d69b 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -53,29 +53,27 @@ static void
 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
                              void *opaque, Error **errp)
 {
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
-    Error *local_err = NULL;
     uint64_t value;
 
     if (host_memory_backend_mr_inited(backend)) {
-        error_setg(&local_err, "cannot change property %s of %s ",
+        error_setg(errp, "cannot change property %s of %s ",
                    name, object_get_typename(obj));
-        goto out;
+        return;
     }
 
-    visit_type_size(v, name, &value, &local_err);
-    if (local_err) {
-        goto out;
+    visit_type_size(v, name, &value, errp);
+    if (*errp) {
+        return;
     }
     if (!value) {
-        error_setg(&local_err,
+        error_setg(errp,
                    "property '%s' of %s doesn't take value '%" PRIu64 "'",
                    name, object_get_typename(obj), value);
-        goto out;
+        return;
     }
     backend->size = value;
-out:
-    error_propagate(errp, local_err);
 }
 
 static void
@@ -221,7 +219,7 @@ static bool host_memory_backend_get_prealloc(Object *obj, 
Error **errp)
 static void host_memory_backend_set_prealloc(Object *obj, bool value,
                                              Error **errp)
 {
-    Error *local_err = NULL;
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
     MachineState *ms = MACHINE(qdev_get_machine());
 
@@ -243,9 +241,8 @@ static void host_memory_backend_set_prealloc(Object *obj, 
bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, ms->smp.cpus, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        os_mem_prealloc(fd, ptr, sz, ms->smp.cpus, errp);
+        if (*errp) {
             return;
         }
         backend->prealloc = true;
@@ -311,17 +308,17 @@ size_t host_memory_backend_pagesize(HostMemoryBackend 
*memdev)
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
 {
+    ERRP_AUTO_PROPAGATE();
     HostMemoryBackend *backend = MEMORY_BACKEND(uc);
     HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
     MachineState *ms = MACHINE(qdev_get_machine());
-    Error *local_err = NULL;
     void *ptr;
     uint64_t sz;
 
     if (bc->alloc) {
-        bc->alloc(backend, &local_err);
-        if (local_err) {
-            goto out;
+        bc->alloc(backend, errp);
+        if (*errp) {
+            return;
         }
 
         ptr = memory_region_get_ram_ptr(&backend->mr);
@@ -378,14 +375,12 @@ host_memory_backend_memory_complete(UserCreatable *uc, 
Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            ms->smp.cpus, &local_err);
-            if (local_err) {
-                goto out;
+                            ms->smp.cpus, errp);
+            if (*errp) {
+                return;
             }
         }
     }
-out:
-    error_propagate(errp, local_err);
 }
 
 static bool
-- 
2.21.0


Reply via email to