diff mbox series

[PATCH-for-5.1,v2,37/54] scripts/coccinelle: Add script to catch missing error_propagate() calls

Message ID 20200406174743.16956-38-f4bug@amsat.org
State New
Headers show
Series various: Fix error-propagation with Coccinelle scripts | expand

Commit Message

Philippe Mathieu-Daudé April 6, 2020, 5:47 p.m. UTC
In some places in we put an error into a local Error*, but forget
to check for failure and pass it back to the caller.
Add a Coccinelle patch to catch automatically add the missing code.

Inspired-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 .../add-missing-error_propagate.cocci         | 30 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 31 insertions(+)
 create mode 100644 scripts/coccinelle/add-missing-error_propagate.cocci

Comments

Eric Blake April 6, 2020, 8:06 p.m. UTC | #1
On 4/6/20 12:47 PM, Philippe Mathieu-Daudé wrote:
> In some places in we put an error into a local Error*, but forget
> to check for failure and pass it back to the caller.
> Add a Coccinelle patch to catch automatically add the missing code.

s/catch/catch and/

> 
> Inspired-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>   .../add-missing-error_propagate.cocci         | 30 +++++++++++++++++++
>   MAINTAINERS                                   |  1 +
>   2 files changed, 31 insertions(+)
>   create mode 100644 scripts/coccinelle/add-missing-error_propagate.cocci
> 
> diff --git a/scripts/coccinelle/add-missing-error_propagate.cocci b/scripts/coccinelle/add-missing-error_propagate.cocci
> new file mode 100644
> index 0000000000..7991c9e2c2
> --- /dev/null
> +++ b/scripts/coccinelle/add-missing-error_propagate.cocci
> @@ -0,0 +1,30 @@
> +// Add missing error-propagation code where caller provide a Error* argument

provides

> +//
> +// Copyright: (C) 2020 Philippe Mathieu-Daudé
> +// This work is licensed under the terms of the GNU GPLv2 or later.
> +//
> +// spatch \
> +//  --macro-file scripts/cocci-macro-file.h --include-headers \
> +//  --sp-file scripts/coccinelle/add-missing-error_propagate.cocci \
> +//  --keep-comments --in-place
> +//
> +// Inspired by https://www.mail-archive.com/qemu-devel@nongnu.org/msg691638.html
> +
> +
> +@ add_missing_error_propagate @
> +typedef Error;
> +Error *local_err;
> +identifier func, errp, errfunc1, errfunc2;
> +@@
> +func(..., Error **errp)
> +{
> +    <...
> +    errfunc1(..., &local_err);
> ++   if (local_err) {
> ++       error_propagate(errp, local_err);
> ++       return;

Do we have to ensure that 'func' returns void?  But then again, I guess 
it's easy enough to validate whether things still compile after this 
script makes cleanups.

> ++   }
> +    ... when != local_err
> +    errfunc2(..., &local_err);
> +    ...>
> +}
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 7b58f02efb..14de2a31dc 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -2053,6 +2053,7 @@ F: include/qemu/error-report.h
>   F: qapi/error.json
>   F: util/error.c
>   F: util/qemu-error.c
> +F: scripts/coccinelle/add-missing-error_propagate.cocci
>   F: scripts/coccinelle/err-bad-newline.cocci
>   F: scripts/coccinelle/error-use-after-free.cocci
>   F: scripts/coccinelle/error_propagate_null.cocci
>
diff mbox series

Patch

diff --git a/scripts/coccinelle/add-missing-error_propagate.cocci b/scripts/coccinelle/add-missing-error_propagate.cocci
new file mode 100644
index 0000000000..7991c9e2c2
--- /dev/null
+++ b/scripts/coccinelle/add-missing-error_propagate.cocci
@@ -0,0 +1,30 @@ 
+// Add missing error-propagation code where caller provide a Error* argument
+//
+// Copyright: (C) 2020 Philippe Mathieu-Daudé
+// This work is licensed under the terms of the GNU GPLv2 or later.
+//
+// spatch \
+//  --macro-file scripts/cocci-macro-file.h --include-headers \
+//  --sp-file scripts/coccinelle/add-missing-error_propagate.cocci \
+//  --keep-comments --in-place
+//
+// Inspired by https://www.mail-archive.com/qemu-devel@nongnu.org/msg691638.html
+
+
+@ add_missing_error_propagate @
+typedef Error;
+Error *local_err;
+identifier func, errp, errfunc1, errfunc2;
+@@
+func(..., Error **errp)
+{
+    <...
+    errfunc1(..., &local_err);
++   if (local_err) {
++       error_propagate(errp, local_err);
++       return;
++   }
+    ... when != local_err
+    errfunc2(..., &local_err);
+    ...>
+}
diff --git a/MAINTAINERS b/MAINTAINERS
index 7b58f02efb..14de2a31dc 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2053,6 +2053,7 @@  F: include/qemu/error-report.h
 F: qapi/error.json
 F: util/error.c
 F: util/qemu-error.c
+F: scripts/coccinelle/add-missing-error_propagate.cocci
 F: scripts/coccinelle/err-bad-newline.cocci
 F: scripts/coccinelle/error-use-after-free.cocci
 F: scripts/coccinelle/error_propagate_null.cocci