From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Anthony Liguori <aliguori@us.ibm.com>,
Markus Armbruster <armbru@redhat.com>,
patches@linaro.org
Subject: [Qemu-devel] [PATCH] qom: Reject attempts to add a property that already exists
Date: Tue, 24 Jul 2012 15:30:18 +0100 [thread overview]
Message-ID: <1343140218-23741-1-git-send-email-peter.maydell@linaro.org> (raw)
Reject attempts to add a property to an object if one of
that name already exists. This is always a bug in the caller;
this is merely diagnosing it gracefully rather than behaving
oddly later.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
This patch can't be applied until these two have been:
http://patchwork.ozlabs.org/patch/172885/
http://patchwork.ozlabs.org/patch/172820/
but I think we're probably going to argue about it anyway.
The question really is whether we want to treat attempts to add
a duplicate property as a programming bug (ie the calling code
should either know there are no duplicates or check first with
object_property_find) or whether we want to return a helpful
failure code from this function. In the code at the moment:
* object_property_add() makes no attempt to cope with duplicate
adds (they get added at the end of the list so will be
never found on a subsequent search, and can't be dereferenced
without first deleting the earlier of the two duplicates)
* there's no attempt to handle failure-to-add in any of the
utility helpers like object_property_add_child() (which
will ref the child object regardless)
* no caller of object_property_add() that I can find passes
in anything except NULL for the Error** so if we were to
do an error_set() here rather than assert() then the error
just vanishes into the ether.
* the documentation in object.h for these functions doesnt' define
semantics for attempts to add duplicate properties
So in theory we could define some semantics (eg "return error
if property already exists"), define a new QERR_* constants since
as usual the existing cast of thousands of QERR_* constants are
unsuitable, fix all the callers to correctly handle and propagate
the error, make device_initfn() print an error, and so on.
But I think this patch is much simpler and more effective :-)
qom/object.c | 13 ++++++++++++-
1 files changed, 12 insertions(+), 1 deletions(-)
diff --git a/qom/object.c b/qom/object.c
index 00bb3b0..dceabc0 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -659,7 +659,18 @@ void object_property_add(Object *obj, const char *name, const char *type,
ObjectPropertyRelease *release,
void *opaque, Error **errp)
{
- ObjectProperty *prop = g_malloc0(sizeof(*prop));
+ ObjectProperty *prop;
+
+ QTAILQ_FOREACH(prop, &obj->properties, node) {
+ if (strcmp(prop->name, name) == 0) {
+ /* This is always a bug in the caller */
+ fprintf(stderr, "attempt to set duplicate property %s on object\n",
+ name);
+ assert(0);
+ }
+ }
+
+ prop = g_malloc0(sizeof(*prop));
prop->name = g_strdup(name);
prop->type = g_strdup(type);
--
1.7.5.4
next reply other threads:[~2012-07-24 14:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-24 14:30 Peter Maydell [this message]
2012-07-24 14:56 ` [Qemu-devel] [PATCH] qom: Reject attempts to add a property that already exists Paolo Bonzini
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=1343140218-23741-1-git-send-email-peter.maydell@linaro.org \
--to=peter.maydell@linaro.org \
--cc=aliguori@us.ibm.com \
--cc=armbru@redhat.com \
--cc=patches@linaro.org \
--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).