diff mbox series

[5/5] libpdbg: Return immediate parent if class is NULL

Message ID 20180816055756.1011374-5-amitay@ozlabs.org
State Accepted
Headers show
Series [1/5] libpdbg: Remove unused variable | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch success master/apply_patch Successfully applied
snowpatch_ozlabs/build-multiarch success Test build-multiarch on branch master

Commit Message

Amitay Isaacs Aug. 16, 2018, 5:57 a.m. UTC
This avoids the segfault if class is NULL.  Also, this api can be used
to traverse the tree by explicitly setting class=NULL.

Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
---
 libpdbg/libpdbg.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Alistair Popple Sept. 7, 2018, 3:23 a.m. UTC | #1
Hi Amitay,

What is this needed for? In the targetting model we have we are trying to avoid
relying on overly specific parent/child relationships as they can be brittle,
hence why callers need to specify which specific type/class of parent they are
looking for. There's not much that could be done with an arbitrary parent
anyway.

Although I agree an `assert(class)` would probably assist debug.

- Alistair

On Thursday, 16 August 2018 3:57:56 PM AEST Amitay Isaacs wrote:
> This avoids the segfault if class is NULL.  Also, this api can be used
> to traverse the tree by explicitly setting class=NULL.
> 
> Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> ---
>  libpdbg/libpdbg.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> index 522bec9..810e045 100644
> --- a/libpdbg/libpdbg.c
> +++ b/libpdbg/libpdbg.c
> @@ -89,6 +89,9 @@ struct pdbg_target *pdbg_target_parent(const char *class, struct pdbg_target *ta
>  {
>  	struct pdbg_target *parent;
>  
> +	if (!class)
> +		return target->parent;
> +
>  	for (parent = target->parent; parent && parent->parent; parent = parent->parent) {
>  		if (!strcmp(class, pdbg_target_class_name(parent)))
>  			return parent;
>
Amitay Isaacs Sept. 7, 2018, 4:03 a.m. UTC | #2
On Fri, 2018-09-07 at 13:23 +1000, Alistair Popple wrote:
> Hi Amitay,
> 
> What is this needed for? In the targetting model we have we are
> trying to avoid
> relying on overly specific parent/child relationships as they can be
> brittle,
> hence why callers need to specify which specific type/class of parent
> they are
> looking for. There's not much that could be done with an arbitrary
> parent
> anyway.

Well we need some mechanisms for traverseing the tree.  I am re-using
this function to also do that.  May be we need separate api when we are
just traversing the tree and when we are searching for targets based on
class.

I needed this functionality in testing to go up and down the device
tree from a node.

> 
> Although I agree an `assert(class)` would probably assist debug.
> 
> - Alistair
> 
> On Thursday, 16 August 2018 3:57:56 PM AEST Amitay Isaacs wrote:
> > This avoids the segfault if class is NULL.  Also, this api can be
> > used
> > to traverse the tree by explicitly setting class=NULL.
> > 
> > Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> > ---
> >  libpdbg/libpdbg.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> > index 522bec9..810e045 100644
> > --- a/libpdbg/libpdbg.c
> > +++ b/libpdbg/libpdbg.c
> > @@ -89,6 +89,9 @@ struct pdbg_target *pdbg_target_parent(const char
> > *class, struct pdbg_target *ta
> >  {
> >  	struct pdbg_target *parent;
> >  
> > +	if (!class)
> > +		return target->parent;
> > +
> >  	for (parent = target->parent; parent && parent->parent; parent
> > = parent->parent) {
> >  		if (!strcmp(class, pdbg_target_class_name(parent)))
> >  			return parent;
> > 
> 
> 

Amitay.
Alistair Popple Sept. 7, 2018, 5:23 a.m. UTC | #3
On Friday, 7 September 2018 2:03:45 PM AEST Amitay Isaacs wrote:
> On Fri, 2018-09-07 at 13:23 +1000, Alistair Popple wrote:
> > Hi Amitay,
> > 
> > What is this needed for? In the targetting model we have we are
> > trying to avoid
> > relying on overly specific parent/child relationships as they can be
> > brittle,
> > hence why callers need to specify which specific type/class of parent
> > they are
> > looking for. There's not much that could be done with an arbitrary
> > parent
> > anyway.
> 
> Well we need some mechanisms for traverseing the tree.  I am re-using
> this function to also do that.  May be we need separate api when we are
> just traversing the tree and when we are searching for targets based on
> class.
> 
> I needed this functionality in testing to go up and down the device
> tree from a node.

You could just use pdbg_for_each_child_target() to traverse down the tree.
Although the existance of this functionality negates my previous argument about
relying on searching for specific classes/types so I'll merge this (which is
just the reverse of what we already have) as well. Thanks!

- Alistair

> > 
> > Although I agree an `assert(class)` would probably assist debug.
> > 
> > - Alistair
> > 
> > On Thursday, 16 August 2018 3:57:56 PM AEST Amitay Isaacs wrote:
> > > This avoids the segfault if class is NULL.  Also, this api can be
> > > used
> > > to traverse the tree by explicitly setting class=NULL.
> > > 
> > > Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> > > ---
> > >  libpdbg/libpdbg.c | 3 +++
> > >  1 file changed, 3 insertions(+)
> > > 
> > > diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
> > > index 522bec9..810e045 100644
> > > --- a/libpdbg/libpdbg.c
> > > +++ b/libpdbg/libpdbg.c
> > > @@ -89,6 +89,9 @@ struct pdbg_target *pdbg_target_parent(const char
> > > *class, struct pdbg_target *ta
> > >  {
> > >  	struct pdbg_target *parent;
> > >  
> > > +	if (!class)
> > > +		return target->parent;
> > > +
> > >  	for (parent = target->parent; parent && parent->parent; parent
> > > = parent->parent) {
> > >  		if (!strcmp(class, pdbg_target_class_name(parent)))
> > >  			return parent;
> > > 
> > 
> > 
> 
> Amitay.
>
diff mbox series

Patch

diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 522bec9..810e045 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -89,6 +89,9 @@  struct pdbg_target *pdbg_target_parent(const char *class, struct pdbg_target *ta
 {
 	struct pdbg_target *parent;
 
+	if (!class)
+		return target->parent;
+
 	for (parent = target->parent; parent && parent->parent; parent = parent->parent) {
 		if (!strcmp(class, pdbg_target_class_name(parent)))
 			return parent;