From 079cd6ebff2168148dc38a52711ca31d9d0cb878 Mon Sep 17 00:00:00 2001 From: Laura Abbott <lauraa@codeaurora.org> Date: Thu, 13 Feb 2014 14:09:10 -0800 Subject: [PATCH] linux-ramdump-parser-v2: Detect corruption in rb_tree Red/Black trees are supposed to be trees, not directed cyclic graphs. Sometimes trees in ramdumps may become corrupted though, resulting in cycles. Detect these cycles in the tree to avoid looping forever. Change-Id: I68cf018c6786b3c6947789a18c63c7a3a1f8d7bc --- linux-ramdump-parser-v2/rb_tree.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/linux-ramdump-parser-v2/rb_tree.py b/linux-ramdump-parser-v2/rb_tree.py index 8a67326..b7a5d33 100644 --- a/linux-ramdump-parser-v2/rb_tree.py +++ b/linux-ramdump-parser-v2/rb_tree.py @@ -1,4 +1,4 @@ -# Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. +# Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 and @@ -30,14 +30,21 @@ class RbTreeWalker(object): self.left_offset = self.ram_dump.field_offset( 'struct rb_node', 'rb_left') - def walk(self, node, func): + def _walk(self, node, func, seen): if node != 0: left_node_addr = node + self.left_offset left_node = self.ram_dump.read_word(left_node_addr) - self.walk(left_node, func) + if left_node not in seen: + seen.append(left_node) + self._walk(left_node, func, seen) func(node) right_node_addr = node + self.right_offset right_node = self.ram_dump.read_word(right_node_addr) - self.walk(right_node, func) + if right_node not in seen: + seen.append(right_node) + self._walk(right_node, func, seen) + + def walk(self, node, func): + self._walk(node, func, []) -- GitLab