qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [PATCH v2 8/8] hw: enforce use of static, const string with qdev_new()
Date: Mon, 11 Nov 2024 15:55:55 +0000	[thread overview]
Message-ID: <20241111155555.90091-9-berrange@redhat.com> (raw)
In-Reply-To: <20241111155555.90091-1-berrange@redhat.com>

Since qdev_new() will assert(), it should only be used in scenarios
where the caller knows exactly what type it is asking to be created,
and can thus be confident in avoiding abstract types.

Enforce this by using a macro wrapper which types to paste "" to the
type name. This will generate a compile error if not passed a static
const string, forcing callers to use qdev_new_dynamic() instead.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 hw/core/qdev.c         |  6 ++++--
 include/hw/qdev-core.h | 24 ++++++++++++++++++++++--
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 10a7b87c3d..d561478437 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -144,7 +144,8 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)
     return true;
 }
 
-DeviceState *qdev_new(const char *name)
+/* Only to be called via the 'qdev_new' macro */
+DeviceState *qdev_new_internal(const char *name)
 {
     return DEVICE(object_new_dynamic(name, &error_abort));
 }
@@ -154,7 +155,8 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp)
     return DEVICE(object_new_dynamic(name, errp));
 }
 
-DeviceState *qdev_try_new(const char *name)
+/* Only to be called via the 'qdev_try_new' macro */
+DeviceState *qdev_try_new_internal(const char *name)
 {
     ObjectClass *oc = module_object_class_by_name(name);
     if (!oc) {
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 68ebaec058..6d9f6ba805 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -448,7 +448,17 @@ compat_props_add(GPtrArray *arr,
  *
  * Return: a derived DeviceState object with a reference count of 1.
  */
-DeviceState *qdev_new(const char *name);
+
+/*
+ * NB, qdev_new_internal is just an internal helper, wrapped by
+ * the qdev_new() macro which prevents invokation unless given
+ * a static, const string.
+ *
+ * Code should call qdev_new(), or qdev_new_dynamic(), not
+ * qdev_new_internal().
+ */
+DeviceState *qdev_new_internal(const char *name);
+#define qdev_new(name) qdev_new_internal(name "")
 
 /**
  * qdev_new_dynamic: Create a device on the heap
@@ -487,7 +497,17 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp);
  * Return: a derived DeviceState object with a reference count of 1 or
  * NULL if type @name does not exist.
  */
-DeviceState *qdev_try_new(const char *name);
+
+/*
+ * NB, qdev_try_new_internal is just an internal helper, wrapped by
+ * the qdev_try_new() macro which prevents invokation unless given
+ * a static, const string.
+ *
+ * Code should call qdev_try_new(), or qdev_try_new_dynamic(), not
+ * qdev_try_new_internal().
+ */
+DeviceState *qdev_try_new_internal(const char *name);
+#define qdev_try_new(name) qdev_try_new_internal(name "")
 
 /**
  * qdev_try_new_dynamic: Try to create a device on the heap
-- 
2.46.0



      parent reply	other threads:[~2024-11-11 15:57 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-11 15:55 [PATCH v2 0/8] Require error handling for dynamically created objects Daniel P. Berrangé
2024-11-11 15:55 ` [PATCH v2 1/8] qom: refactor checking abstract property when creating instances Daniel P. Berrangé
2024-11-14 19:58   ` Peter Xu
2024-11-15 10:41     ` Daniel P. Berrangé
2024-11-11 15:55 ` [PATCH v2 2/8] qom: allow failure of object_new_with_class Daniel P. Berrangé
2024-11-14 20:04   ` Peter Xu
2024-11-11 15:55 ` [PATCH v2 3/8] qom: introduce object_new_dynamic() Daniel P. Berrangé
2024-11-14 20:15   ` Peter Xu
2024-11-11 15:55 ` [PATCH v2 4/8] convert code to object_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-14 20:24   ` Peter Xu
2024-11-11 15:55 ` [PATCH v2 5/8] qom: enforce use of static, const string with object_new() Daniel P. Berrangé
2024-11-14 20:28   ` Peter Xu
2024-11-11 15:55 ` [PATCH v2 6/8] qom: introduce qdev_new_dynamic() Daniel P. Berrangé
2024-11-14 20:47   ` Peter Xu
2024-11-15 17:26     ` Daniel P. Berrangé
2024-11-11 15:55 ` [PATCH v2 7/8] convert code to qdev_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-14 21:00   ` Peter Xu
2024-11-11 15:55 ` Daniel P. Berrangé [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241111155555.90091-9-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=armbru@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).