From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Peter Xu" <peterx@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [PATCH v3 6/9] qom: enforce use of static, const string with object_new()
Date: Fri, 15 Nov 2024 17:25:18 +0000 [thread overview]
Message-ID: <20241115172521.504102-7-berrange@redhat.com> (raw)
In-Reply-To: <20241115172521.504102-1-berrange@redhat.com>
Since object_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 object_new_dynamic() instead.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
include/qom/object.h | 12 +++++++++++-
qom/object.c | 3 ++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/include/qom/object.h b/include/qom/object.h
index 4fc01336c4..2d5a0d84b5 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -637,7 +637,17 @@ Object *object_new_with_class(ObjectClass *klass, Error **errp);
*
* Returns: The newly allocated and instantiated object.
*/
-Object *object_new(const char *typename);
+
+/*
+ * NB, object_new_internal is just an internal helper, wrapped by
+ * the object_new() macro which prevents invokation unless given
+ * a static, const string.
+ *
+ * Code should call object_new(), or object_new_dynamic(), not
+ * object_new_internal().
+ */
+Object *object_new_internal(const char *typename);
+#define object_new(typename) object_new_internal(typename "")
/**
* object_new_dynamic:
diff --git a/qom/object.c b/qom/object.c
index 42ef40a1fd..ee56710348 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -799,7 +799,8 @@ Object *object_new_with_class(ObjectClass *klass, Error **errp)
return object_new_with_type(klass->type, errp);
}
-Object *object_new(const char *typename)
+/* Only to be called via the 'object_new' macro */
+Object *object_new_internal(const char *typename)
{
TypeImpl *ti = type_get_or_load_by_name(typename, &error_fatal);
--
2.46.0
next prev parent reply other threads:[~2024-11-15 17:26 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-15 17:25 [PATCH v3 0/9] Require error handling for dynamically created objects Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 1/9] hw: eliminate qdev_try_new, isa_try_new & usb_try_new Daniel P. Berrangé
2024-11-15 17:54 ` Peter Xu
2024-11-15 18:34 ` Daniel P. Berrangé
2024-12-03 15:30 ` Markus Armbruster
2024-12-05 16:21 ` Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 2/9] qom: refactor checking abstract property when creating instances Daniel P. Berrangé
2024-11-15 17:54 ` Peter Xu
2024-11-15 17:25 ` [PATCH v3 3/9] qom: allow failure of object_new_with_class Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 4/9] qom: introduce object_new_dynamic() Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 5/9] convert code to object_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-15 17:25 ` Daniel P. Berrangé [this message]
2024-11-15 17:25 ` [PATCH v3 7/9] qom: introduce qdev_new_dynamic() Daniel P. Berrangé
2024-11-15 17:55 ` Peter Xu
2024-11-15 17:25 ` [PATCH v3 8/9] convert code to qdev_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 9/9] hw: enforce use of static, const string with qdev_new() Daniel P. Berrangé
2024-11-15 17:55 ` Peter Xu
2024-12-04 11:07 ` [PATCH v3 0/9] Require error handling for dynamically created objects Markus Armbruster
2024-12-05 16:04 ` Daniel P. Berrangé
2024-12-06 8:25 ` Markus Armbruster
2024-12-06 10:57 ` Daniel P. Berrangé
2024-12-07 7:37 ` Markus Armbruster
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=20241115172521.504102-7-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).