From patchwork Wed Jul 10 10:19:28 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2825601 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 65D449F7D6 for ; Wed, 10 Jul 2013 10:19:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8C63E2013A for ; Wed, 10 Jul 2013 10:19:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3178120137 for ; Wed, 10 Jul 2013 10:19:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754033Ab3GJKTO (ORCPT ); Wed, 10 Jul 2013 06:19:14 -0400 Received: from perceval.ideasonboard.com ([95.142.166.194]:39966 "EHLO perceval.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753991Ab3GJKTM (ORCPT ); Wed, 10 Jul 2013 06:19:12 -0400 Received: from localhost.localdomain (unknown [193.120.41.118]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 86FF335A4C; Wed, 10 Jul 2013 12:18:59 +0200 (CEST) From: Laurent Pinchart To: linux-media@vger.kernel.org Cc: linux-sh@vger.kernel.org Subject: [PATCH 1/5] media: Fix circular graph traversal Date: Wed, 10 Jul 2013 12:19:28 +0200 Message-Id: <1373451572-3892-2-git-send-email-laurent.pinchart+renesas@ideasonboard.com> X-Mailer: git-send-email 1.8.1.5 In-Reply-To: <1373451572-3892-1-git-send-email-laurent.pinchart+renesas@ideasonboard.com> References: <1373451572-3892-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.2 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. Cc: Sakari Ailus 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 e1cd132..9084205 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; }