From: Peter Xu <peterx@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [RFC 5/5] qom: enforce use of static, const string with object_new()
Date: Thu, 31 Oct 2024 15:32:14 -0400 [thread overview]
Message-ID: <ZyPbPtkMBClKLa2Y@x1n> (raw)
In-Reply-To: <20241031155350.3240361-6-berrange@redhat.com>
On Thu, Oct 31, 2024 at 03:53:50PM +0000, Daniel P. Berrangé wrote:
> 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.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> include/qom/object.h | 12 +++++++++++-
> qom/object.c | 2 +-
> 2 files changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 8c2f3551c5..6a21cb6ca0 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_helper 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_helper().
> + */
> +Object *object_new_helper(const char *typename);
Nit; personally I'd call it object_new_internal(). No strong opinions.
> +#define object_new(typename) object_new_static(typename "")
Interesting trick on const check.. I see why the test cases need change
now. Feel free to ignore the comment there then..
Could be an improvement to enforce error checks on new dynamic allocations.
This should be better than my patch 1 indeed.
Thanks,
--
Peter Xu
next prev parent reply other threads:[~2024-10-31 19:33 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-31 15:53 [RFC 0/5] RFC: require error handling for dynamically created objects Daniel P. Berrangé
2024-10-31 15:53 ` [RFC 1/5] qom: refactor checking abstract property when creating instances Daniel P. Berrangé
2024-10-31 15:53 ` [RFC 2/5] qom: allow failure of object_new_with_class Daniel P. Berrangé
2024-10-31 19:09 ` Peter Xu
2024-11-01 11:29 ` Daniel P. Berrangé
2024-10-31 15:53 ` [RFC 3/5] convert code to object_new_dynamic() where appropriate Daniel P. Berrangé
2024-10-31 19:21 ` Peter Xu
2024-11-01 11:32 ` Daniel P. Berrangé
2024-10-31 15:53 ` [RFC 4/5] qom: introduce object_new_dynamic() Daniel P. Berrangé
2024-10-31 19:22 ` Peter Xu
2024-11-01 11:32 ` Daniel P. Berrangé
2024-10-31 15:53 ` [RFC 5/5] qom: enforce use of static, const string with object_new() Daniel P. Berrangé
2024-10-31 19:32 ` Peter Xu [this message]
2024-10-31 19:46 ` [RFC 0/5] RFC: require error handling for dynamically created objects Peter Xu
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=ZyPbPtkMBClKLa2Y@x1n \
--to=peterx@redhat.com \
--cc=berrange@redhat.com \
--cc=pbonzini@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).