diff mbox series

[v2,1/8] qom: refactor checking abstract property when creating instances

Message ID 20241111155555.90091-2-berrange@redhat.com (mailing list archive)
State New
Headers show
Series Require error handling for dynamically created objects | expand

Commit Message

Daniel P. Berrangé Nov. 11, 2024, 3:55 p.m. UTC
Push an Error object into object_initialize_with_type, so that
reporting of attempts to create an abstract type is handled at
the lowest level.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 qom/object.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

Comments

Peter Xu Nov. 14, 2024, 7:58 p.m. UTC | #1
On Mon, Nov 11, 2024 at 03:55:48PM +0000, Daniel P. Berrangé wrote:
> @@ -753,7 +761,7 @@ typedef union {
>  } qemu_max_align_t;
>  #endif
>  
> -static Object *object_new_with_type(Type type)
> +static Object *object_new_with_type(Type type, Error **errp)
>  {
>      Object *obj;
>      size_t size, align;
> @@ -777,7 +785,10 @@ static Object *object_new_with_type(Type type)
>          obj_free = qemu_vfree;
>      }
>  
> -    object_initialize_with_type(obj, size, type);
> +    if (!object_initialize_with_type(obj, size, type, errp)) {
> +        g_free(obj);

obj_free(obj)?

> +        return NULL;
> +    }
>      obj->free = obj_free;
>  
>      return obj;
diff mbox series

Patch

diff --git a/qom/object.c b/qom/object.c
index 9edc06d391..dc125e55bc 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -559,14 +559,20 @@  static void object_class_property_init_all(Object *obj)
     }
 }
 
-static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
+static bool object_initialize_with_type(Object *obj, size_t size,
+                                        TypeImpl *type, Error **errp)
 {
     type_initialize(type);
 
     g_assert(type->instance_size >= sizeof(Object));
-    g_assert(type->abstract == false);
     g_assert(size >= type->instance_size);
 
+    if (type->abstract) {
+        error_setg(errp, "Abstract type '%s' cannot be instantiated",
+                   type->name);
+        return false;
+    }
+
     memset(obj, 0, type->instance_size);
     obj->class = type->class;
     object_ref(obj);
@@ -575,13 +581,15 @@  static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type
                                             NULL, object_property_free);
     object_init_with_type(obj, type);
     object_post_init_with_type(obj, type);
+
+    return true;
 }
 
 void object_initialize(void *data, size_t size, const char *typename)
 {
     TypeImpl *type = type_get_or_load_by_name(typename, &error_fatal);
 
-    object_initialize_with_type(data, size, type);
+    object_initialize_with_type(data, size, type, &error_abort);
 }
 
 bool object_initialize_child_with_props(Object *parentobj,
@@ -753,7 +761,7 @@  typedef union {
 } qemu_max_align_t;
 #endif
 
-static Object *object_new_with_type(Type type)
+static Object *object_new_with_type(Type type, Error **errp)
 {
     Object *obj;
     size_t size, align;
@@ -777,7 +785,10 @@  static Object *object_new_with_type(Type type)
         obj_free = qemu_vfree;
     }
 
-    object_initialize_with_type(obj, size, type);
+    if (!object_initialize_with_type(obj, size, type, errp)) {
+        g_free(obj);
+        return NULL;
+    }
     obj->free = obj_free;
 
     return obj;
@@ -785,14 +796,14 @@  static Object *object_new_with_type(Type type)
 
 Object *object_new_with_class(ObjectClass *klass)
 {
-    return object_new_with_type(klass->type);
+    return object_new_with_type(klass->type, &error_abort);
 }
 
 Object *object_new(const char *typename)
 {
     TypeImpl *ti = type_get_or_load_by_name(typename, &error_fatal);
 
-    return object_new_with_type(ti);
+    return object_new_with_type(ti, &error_abort);
 }
 
 
@@ -829,11 +840,10 @@  Object *object_new_with_propv(const char *typename,
         return NULL;
     }
 
-    if (object_class_is_abstract(klass)) {
-        error_setg(errp, "object type '%s' is abstract", typename);
+    obj = object_new_with_type(klass->type, errp);
+    if (!obj) {
         return NULL;
     }
-    obj = object_new_with_type(klass->type);
 
     if (!object_set_propv(obj, errp, vargs)) {
         goto error;