Patchwork [v4,1/2] qom: Detect attempts to add a property that already exists

login
register
mail settings
Submitter Peter Maydell
Date Oct. 25, 2012, 3:22 p.m.
Message ID <1351178549-5699-2-git-send-email-peter.maydell@linaro.org>
Download mbox | patch
Permalink /patch/194237/
State New
Headers show

Comments

Peter Maydell - Oct. 25, 2012, 3:22 p.m.
Detect attempts to add a property to an object if one of
that name already exists, and report them as critical
errors. In particular, for static properties (eg qdev
Property arrays) this will manifest as an abort() with
a useful error message.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 qom/object.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Markus Armbruster - Oct. 25, 2012, 5:27 p.m.
Peter Maydell <peter.maydell@linaro.org> writes:

> Detect attempts to add a property to an object if one of
> that name already exists, and report them as critical
> errors. In particular, for static properties (eg qdev
> Property arrays) this will manifest as an abort() with
> a useful error message.

What's critical about this error?  It looks like any other error to
me...  Leftover from previous iterations, perhaps?
Peter Maydell - Oct. 25, 2012, 5:35 p.m.
On 25 October 2012 18:27, Markus Armbruster <armbru@redhat.com> wrote:
> Peter Maydell <peter.maydell@linaro.org> writes:
>
>> Detect attempts to add a property to an object if one of
>> that name already exists, and report them as critical
>> errors. In particular, for static properties (eg qdev
>> Property arrays) this will manifest as an abort() with
>> a useful error message.
>
> What's critical about this error?  It looks like any other error to
> me...  Leftover from previous iterations, perhaps?

Rats, yes, I forgot to update the commit message. Should read:

Detect attempts to add a property to an object if one of
that name already exists, and report them as errors.

-- PMM
Anthony Liguori - Oct. 25, 2012, 6:39 p.m.
Peter Maydell <peter.maydell@linaro.org> writes:

> Detect attempts to add a property to an object if one of
> that name already exists, and report them as critical
> errors. In particular, for static properties (eg qdev
> Property arrays) this will manifest as an abort() with
> a useful error message.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

Regards,

Anthony Liguori

> ---
>  qom/object.c |   13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/qom/object.c b/qom/object.c
> index e3e9242..a9dfc8c 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -620,7 +620,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) {
> +            error_setg(errp, "attempt to add duplicate property '%s'"
> +                       " to object (type '%s')\n", name,
> +                       object_get_typename(obj));
> +            return;
> +        }
> +    }
> +
> +    prop = g_malloc0(sizeof(*prop));
>  
>      prop->name = g_strdup(name);
>      prop->type = g_strdup(type);
> -- 
> 1.7.9.5

Patch

diff --git a/qom/object.c b/qom/object.c
index e3e9242..a9dfc8c 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -620,7 +620,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) {
+            error_setg(errp, "attempt to add duplicate property '%s'"
+                       " to object (type '%s')\n", name,
+                       object_get_typename(obj));
+            return;
+        }
+    }
+
+    prop = g_malloc0(sizeof(*prop));
 
     prop->name = g_strdup(name);
     prop->type = g_strdup(type);