Message ID | 20180530071757.9112-2-aik@ozlabs.ru |
---|---|
State | New |
Headers | show |
Series | [qemu,v3,1/2] object: Handle objects with no parents | expand |
On 30/05/2018 09:17, Alexey Kardashevskiy wrote: > At the moment object_get_canonical_path() crashes if the object or one > of its parents does not have a parent, for example, a KVM accelerator > object. > > This adds a check for obj!=NULL in a loop to prevent the crash. > In order not to return a wrong path, this checks for currently resolved > partial path and does not add a leading slash to tell the reader that > the path is partial as the owner object is detached. > > Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru> > --- > > I have not tested the case with obj==NULL and path!=NULL as this is > for objects which have parents which are not attached to the root > and we do not have such objects in current QEMU afaict but I kept it > just in case. > > --- > Changes: > v3: > * do not check for obj->parent > * return NULL or incomplete path depending on the situation > --- > qom/object.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/qom/object.c b/qom/object.c > index 0fc9720..05138ba 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -1669,7 +1669,7 @@ gchar *object_get_canonical_path(Object *obj) > Object *root = object_get_root(); > char *newpath, *path = NULL; > > - while (obj != root) { > + while (obj && obj != root) { > char *component = object_get_canonical_path_component(obj); > > if (path) { > @@ -1684,7 +1684,13 @@ gchar *object_get_canonical_path(Object *obj) > obj = obj->parent; > } > > - newpath = g_strdup_printf("/%s", path ? path : ""); > + if (obj && path) { > + newpath = g_strdup_printf("/%s", path); > + } else if (path) { > + newpath = g_strdup(path); > + } else { > + newpath = NULL; > + } > g_free(path); > > return newpath; I think this is still wrong, as you wouldn't be able to get back these objects later (e.g. by assigning the result to a link<> property). I've posted an alternative patch. Paolo
diff --git a/qom/object.c b/qom/object.c index 0fc9720..05138ba 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1669,7 +1669,7 @@ gchar *object_get_canonical_path(Object *obj) Object *root = object_get_root(); char *newpath, *path = NULL; - while (obj != root) { + while (obj && obj != root) { char *component = object_get_canonical_path_component(obj); if (path) { @@ -1684,7 +1684,13 @@ gchar *object_get_canonical_path(Object *obj) obj = obj->parent; } - newpath = g_strdup_printf("/%s", path ? path : ""); + if (obj && path) { + newpath = g_strdup_printf("/%s", path); + } else if (path) { + newpath = g_strdup(path); + } else { + newpath = NULL; + } g_free(path); return newpath;
At the moment object_get_canonical_path() crashes if the object or one of its parents does not have a parent, for example, a KVM accelerator object. This adds a check for obj!=NULL in a loop to prevent the crash. In order not to return a wrong path, this checks for currently resolved partial path and does not add a leading slash to tell the reader that the path is partial as the owner object is detached. Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru> --- I have not tested the case with obj==NULL and path!=NULL as this is for objects which have parents which are not attached to the root and we do not have such objects in current QEMU afaict but I kept it just in case. --- Changes: v3: * do not check for obj->parent * return NULL or incomplete path depending on the situation --- qom/object.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)