Skip to contentSkip to conversation

qom: In function object_set_link_property(), first call object_ref(), then object_unref().

PATCH In review [email protected] 2 participants 3 messages Feb 22, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qom: In function object_set_link_property(), first call object_ref(), then object_unref().

From: Alexander Barabash <[email protected]>

In the old implementation, if the new value of the property links to the same object, as the old value, that object is first unref-ed, and then ref-ed. This leads to unintended deinitialization of that object.

In the new implementation, this is fixed.

---
 qom/object.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
qom/object.c +13 -5
index 941c291..d1b3ac7 100644
@@ -892,19 +892,19 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
                                      const char *name, Error **errp)
 {
     Object **child = opaque;
+    Object *old_target;
     bool ambiguous = false;
     const char *type;
     char *path;
     gchar *target_type;
+    bool clear_old_target = true;
 
     type = object_property_get_type(obj, name, NULL);
 
     visit_type_str(v, &path, name, errp);
 
-    if (*child) {
-        object_unref(*child);
-        *child = NULL;
-    }
+    old_target = *child;
+    *child = NULL;
 
     if (strcmp(path, "") != 0) {
         Object *target;
@@ -916,7 +916,11 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
         if (ambiguous) {
             error_set(errp, QERR_AMBIGUOUS_PATH, path);
         } else if (target) {
-            object_ref(target);
+            if (target != old_target) {
+                object_ref(target);
+            } else {
+                clear_old_target = false;
+            }
             *child = target;
         } else {
             target = object_resolve_path(path, &ambiguous);
@@ -930,6 +934,10 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
     }
 
     g_free(path);
+
+    if (clear_old_target && (old_target != NULL)) {
+        object_unref(old_target);
+    }
 }
 
 void object_property_add_link(Object *obj, const char *name,
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 28 lines · On 02/22/2012 06:13 PM, [email protected] wrote:

From: Alexander Barabash <[email protected]>

In the old implementation, if the new value of the property links to the same object, as the old value, that object is first unref-ed, and then ref-ed. This leads to unintended deinitialization of that object.

In the new implementation, this is fixed.

---
 qom/object.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
qom/object.c +2 -0
index 941c291..d1b3ac7 100644
@@ -892,19 +892,19 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
                                      const char *name, Error **errp)
 {
     Object **child = opaque;
+    Object *old_target;
     bool ambiguous = false;
     const char *type;
     char *path;
     gchar *target_type;
+    bool clear_old_target = true;
 

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 907

Quoted 10 lines
qom/object.c +2 -4
     type = object_property_get_type(obj, name, NULL);
 
     visit_type_str(v, &path, name, errp);
 
-    if (*child) {
-        object_unref(*child);
-        *child = NULL;
-    }
+    old_target = *child;
+    *child = NULL;

You can just remove the unref here...

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 920

Quoted 9 lines
qom/object.c +2 -1
     if (strcmp(path, "") != 0) {
         Object *target;
@@ -916,7 +916,11 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
         if (ambiguous) {
             error_set(errp, QERR_AMBIGUOUS_PATH, path);
         } else if (target) {
-            object_ref(target);
+            if (target != old_target) {
+                object_ref(target);

... leave the unconditional ref to target here...

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 939

Quoted 13 lines
qom/object.c +6 -0
+            } else {
+                clear_old_target = false;
+            }
             *child = target;
         } else {
             target = object_resolve_path(path, &ambiguous);
@@ -930,6 +934,10 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
     }
 
     g_free(path);
+
+    if (clear_old_target && (old_target != NULL)) {
+        object_unref(old_target);

... and leave this unref on old_target, without the need for clear_old_target.

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 943

Quoted 4 lines
qom/object.c +1 -0
+    }
 }
 
 void object_property_add_link(Object *obj, const char *name,

Paolo

#2 lore raw
Quoted 68 lines · Paolo Bonzini
Quoted 28 lines · On 02/22/2012 06:13 PM, [email protected] wrote:

From: Alexander Barabash<[email protected]>

In the old implementation, if the new value of the property links to the same object, as the old value, that object is first unref-ed, and then ref-ed. This leads to unintended deinitialization of that object.

In the new implementation, this is fixed.

---
  qom/object.c |   18 +++++++++++++-----
  1 files changed, 13 insertions(+), 5 deletions(-)
qom/object.c +2 -0
index 941c291..d1b3ac7 100644
@@ -892,19 +892,19 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
                                       const char *name, Error **errp)
  {
      Object **child = opaque;
+    Object *old_target;
      bool ambiguous = false;
      const char *type;
      char *path;
      gchar *target_type;
+    bool clear_old_target = true;
 

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 907

Quoted 10 lines
qom/object.c +2 -4
      type = object_property_get_type(obj, name, NULL);
 
      visit_type_str(v,&path, name, errp);
 
-    if (*child) {
-        object_unref(*child);
-        *child = NULL;
-    }
+    old_target = *child;
+    *child = NULL;

You can just remove the unref here...

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 920

Quoted 9 lines
qom/object.c +2 -1
      if (strcmp(path, "") != 0) {
          Object *target;
@@ -916,7 +916,11 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
          if (ambiguous) {
              error_set(errp, QERR_AMBIGUOUS_PATH, path);
          } else if (target) {
-            object_ref(target);
+            if (target != old_target) {
+                object_ref(target);

... leave the unconditional ref to target here...

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 939

Quoted 13 lines
qom/object.c +6 -0
+            } else {
+                clear_old_target = false;
+            }
              *child = target;
          } else {
              target = object_resolve_path(path,&ambiguous);
@@ -930,6 +934,10 @@ static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
      }
 
      g_free(path);
+
+    if (clear_old_target&&  (old_target != NULL)) {
+        object_unref(old_target);

... and leave this unref on old_target, without the need for clear_old_target.

Quoted 5 lines

qom/object.c static void object_set_link_property(Object *obj, Visitor *v, void *opaque, · line 943

Quoted 4 lines
qom/object.c +1 -0
+    }
  }
 
  void object_property_add_link(Object *obj, const char *name,

Paolo

Agreed.
Alex