From patchwork Wed Jul 17 14:54:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2828700 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7551CC0AB2 for ; Wed, 17 Jul 2013 14:54:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 61AC6201CA for ; Wed, 17 Jul 2013 14:54:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A76FA20174 for ; Wed, 17 Jul 2013 14:54:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755744Ab3GQOyF (ORCPT ); Wed, 17 Jul 2013 10:54:05 -0400 Received: from perceval.ideasonboard.com ([95.142.166.194]:33692 "EHLO perceval.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755682Ab3GQOyC (ORCPT ); Wed, 17 Jul 2013 10:54:02 -0400 Received: from avalon.ideasonboard.com (unknown [91.178.223.113]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 62AE335A4B; Wed, 17 Jul 2013 16:53:48 +0200 (CEST) From: Laurent Pinchart To: linux-media@vger.kernel.org Cc: linux-sh@vger.kernel.org, Hans Verkuil , Sakari Ailus Subject: [PATCH v2 1/5] media: Fix circular graph traversal Date: Wed, 17 Jul 2013 16:54:38 +0200 Message-Id: <1374072882-14598-2-git-send-email-laurent.pinchart+renesas@ideasonboard.com> X-Mailer: git-send-email 1.8.1.5 In-Reply-To: <1374072882-14598-1-git-send-email-laurent.pinchart+renesas@ideasonboard.com> References: <1374072882-14598-1-git-send-email-laurent.pinchart+renesas@ideasonboard.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The graph traversal API (media_entity_graph_walk_*) will fail to correctly walk the graph when circular links exist. Fix it by checking whether an entity is already present in the stack before pushing it. Signed-off-by: Laurent Pinchart --- drivers/media/media-entity.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/media/media-entity.c b/drivers/media/media-entity.c index cb30ffb..c8aba5e 100644 --- a/drivers/media/media-entity.c +++ b/drivers/media/media-entity.c @@ -121,9 +121,9 @@ static struct media_entity *stack_pop(struct media_entity_graph *graph) return entity; } -#define stack_peek(en) ((en)->stack[(en)->top - 1].entity) -#define link_top(en) ((en)->stack[(en)->top].link) -#define stack_top(en) ((en)->stack[(en)->top].entity) +#define stack_peek(en, i) ((en)->stack[i].entity) +#define link_top(en) ((en)->stack[(en)->top].link) +#define stack_top(en) ((en)->stack[(en)->top].entity) /** * media_entity_graph_walk_start - Start walking the media graph at a given entity @@ -159,6 +159,8 @@ EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); struct media_entity * media_entity_graph_walk_next(struct media_entity_graph *graph) { + unsigned int i; + if (stack_top(graph) == NULL) return NULL; @@ -181,8 +183,13 @@ media_entity_graph_walk_next(struct media_entity_graph *graph) /* Get the entity in the other end of the link . */ next = media_entity_other(entity, link); - /* Was it the entity we came here from? */ - if (next == stack_peek(graph)) { + /* Is the entity already in the path? */ + for (i = 1; i < graph->top; ++i) { + if (next == stack_peek(graph, i)) + break; + } + + if (i < graph->top) { link_top(graph)++; continue; }