[Pdbg] [PATCH] libpdbg: Fix pdbg_target_index

Alistair Popple alistair at popple.id.au
Mon Dec 11 15:41:06 AEDT 2017


The pdbg_target_index is supposed to walk up the tree looking for an index.
Instead it caused segfaults when the current target has no index assigned.
Fix the code up to make it do what it's supposed to.

Signed-off-by: Alistair Popple <alistair at popple.id.au>
---
 libpdbg/libpdbg.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index a971125..6194048 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -102,11 +102,14 @@ void pdbg_disable_target(struct pdbg_target *target)
 /* Searches up the tree and returns the first valid index found */
 uint32_t pdbg_target_index(struct pdbg_target *target)
 {
-	uint32_t index;
-	struct dt_node *dn = target->dn;
+	struct dt_node *dn;
+
+	for (dn = target->dn; dn && dn->target->index == -1; dn = dn->parent);
 
-	for (index = dn->target->index; index == -1; dn = dn->parent);
-	return index;
+	if (!dn)
+		return -1;
+	else
+		return dn->target->index;
 }
 
 /* Searched up the tree for the first target of the right class and returns its index */
-- 
2.11.0



More information about the Pdbg mailing list