Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EE6D1D78339 for ; Mon, 2 Dec 2024 17:31:51 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C1AAD8937F; Mon, 2 Dec 2024 18:31:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.b="uP/19IFf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8996E8935C; Mon, 2 Dec 2024 18:31:45 +0100 (CET) Received: from mx3.securetransport.de (mx3.securetransport.de [IPv6:2a01:4f8:c0c:92be::1]) by phobos.denx.de (Postfix) with ESMTP id 8372C8946E for ; Mon, 2 Dec 2024 18:31:43 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cniedermaier@dh-electronics.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dh-electronics.com; s=dhelectronicscom; t=1733160674; bh=DbteDWRsvKaT/Hs4+vLU/S0mvw/XtVbvACP49+NrYKM=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=uP/19IFf0OKlckxJue1KPpbY6a3teRA3SVBf9Cql2xh1qboVZay3b23WmxFtZY7jw PPOi3SuwIQ+Lg4jhyPLcHmDeqvYQsrWVF61XRG+2T1C6ggJO4i+Rvy2CU64kmpFlvn p0NjmJkiuFX+eqabdDfOaN/fexT6sJSQhvoe4XDi0fqROWd0i/jG+ZtuUfHmVc+p1g bsnlyODNzmNWkX/4O6Ztg0jbOFEOwshvKhN6RbnOBf4Qq99rQVEDgwYpmUuafXh2pH rnaMyqefKn3Bd6k39gyl6e9vcDhE8eh/HuDlgkQkP4TyQtQE8Wf2vNtuIy3t5pY56i S9oEZtaVqPrZg== From: Christoph Niedermaier To: CC: Christoph Niedermaier , Marek Vasut , Tom Rini , Simon Glass , Subject: [PATCH V5 3/4] lib: hashtable: Prevent recursive calling of callback functions Date: Mon, 2 Dec 2024 18:29:49 +0100 Message-ID: <20241202172950.35460-4-cniedermaier@dh-electronics.com> X-klartext: yes In-Reply-To: <20241202172950.35460-1-cniedermaier@dh-electronics.com> References: <20241202172950.35460-1-cniedermaier@dh-electronics.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean In case there are two variables which each implement env callback that performs env_set() on the other variable, the callbacks will call each other recursively until the stack runs out. Prevent such a recursion from happening. Example which triggers this behavior: static int on_foo(...) { env_set("bar", 0); ... } static int on_bar(...) { env_set("foo", 0); ... } U_BOOT_ENV_CALLBACK(foo, on_foo); U_BOOT_ENV_CALLBACK(bar, on_bar); Signed-off-by: Christoph Niedermaier Suggested-by: Marek Vasut --- Cc: Tom Rini Cc: Simon Glass Cc: Marek Vasut Cc: u-boot@dh-electronics.com --- V4: - Add this patch to the series v5: - No functional changes, but changed the order of the patches --- lib/hashtable.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/hashtable.c b/lib/hashtable.c index e8a59e2dca..75c263b505 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -221,11 +221,32 @@ static int do_callback(const struct env_entry *e, const char *name, const char *value, enum env_op op, int flags) { + int ret = 0; + #ifndef CONFIG_XPL_BUILD - if (e->callback) - return e->callback(name, value, op, flags); + static bool in_callback; + + if (!e->callback || in_callback) + return 0; + + /* + * In case there are two variables which each implement env callback + * that performs env_set() on the other variable, the callbacks will + * call each other recursively until the stack runs out. Prevent such + * a recursion from happening. + * + * Example which triggers this behavior: + * static int on_foo(...) { env_set("bar", 0); ... } + * static int on_bar(...) { env_set("foo", 0); ... } + * U_BOOT_ENV_CALLBACK(foo, on_foo); + * U_BOOT_ENV_CALLBACK(bar, on_bar); + */ + in_callback = true; + ret = e->callback(name, value, op, flags); + in_callback = false; #endif - return 0; + + return ret; } /* -- 2.30.2