[PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute

Manos Pitsidianakis posted 2 patches 2 years ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Peter Maydell <peter.maydell@linaro.org>
[PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute
Posted by Manos Pitsidianakis 2 years ago
This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function
attribute `warn_unused_result`. The utility of this attribute is to
ensure functions that return values that need to be inspected are not
ignored by the caller.

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
---
 include/qemu/compiler.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index c797f0d457..7ddbf1f1cf 100644
--- a/include/qemu/compiler.h
+++ b/include/qemu/compiler.h
@@ -212,6 +212,20 @@
 # define QEMU_USED
 #endif
 
+/*
+ * From GCC documentation:
+ *
+ *   The warn_unused_result attribute causes a warning to be emitted if a
+ *   caller of the function with this attribute does not use its return value.
+ *   This is useful for functions where not checking the result is either a
+ *   security problem or always a bug, such as realloc.
+ */
+#if __has_attribute(warn_unused_result)
+# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+# define QEMU_WARN_UNUSED_RESULT
+#endif
+
 /*
  * Ugly CPP trick that is like "defined FOO", but also works in C
  * code.  Useful to replace #ifdef with "if" statements; assumes
-- 
2.39.2
Re: [PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute
Posted by Philippe Mathieu-Daudé 2 years ago
On 10/11/23 10:16, Manos Pitsidianakis wrote:
> This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function
> attribute `warn_unused_result`. The utility of this attribute is to
> ensure functions that return values that need to be inspected are not
> ignored by the caller.
> 
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
>   include/qemu/compiler.h | 14 ++++++++++++++
>   1 file changed, 14 insertions(+)

> +/*
> + * From GCC documentation:
> + *
> + *   The warn_unused_result attribute causes a warning to be emitted if a
> + *   caller of the function with this attribute does not use its return value.
> + *   This is useful for functions where not checking the result is either a
> + *   security problem or always a bug, such as realloc.
> + */
> +#if __has_attribute(warn_unused_result)
> +# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
> +#else
> +# define QEMU_WARN_UNUSED_RESULT
> +#endif

FWIW I sometimes use:

+#if __has_attribute(nonnull)
+# define QEMU_NONNULL(indexes...) __attribute__((nonnull((indexes))))
+#else
+# define QEMU_NONNULL(indexes...)
+#endif

when doing tree-wide refactors, then remove the attribute because
prototypes become really ugly.
Re: [PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute
Posted by Daniel P. Berrangé 2 years ago
On Fri, Nov 10, 2023 at 11:16:38AM +0200, Manos Pitsidianakis wrote:
> This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function
> attribute `warn_unused_result`. The utility of this attribute is to
> ensure functions that return values that need to be inspected are not
> ignored by the caller.
> 
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
>  include/qemu/compiler.h | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
> index c797f0d457..7ddbf1f1cf 100644
> --- a/include/qemu/compiler.h
> +++ b/include/qemu/compiler.h
> @@ -212,6 +212,20 @@
>  # define QEMU_USED
>  #endif
>  
> +/*
> + * From GCC documentation:
> + *
> + *   The warn_unused_result attribute causes a warning to be emitted if a
> + *   caller of the function with this attribute does not use its return value.
> + *   This is useful for functions where not checking the result is either a
> + *   security problem or always a bug, such as realloc.
> + */
> +#if __has_attribute(warn_unused_result)
> +# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
> +#else
> +# define QEMU_WARN_UNUSED_RESULT
> +#endif

GLib already provides us G_GNUC_WARN_UNUSED_RESULT so don't add this.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|