From mboxrd@z Thu Jan 1 00:00:00 1970 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 35684C6FD1D for ; Mon, 20 Mar 2023 08:23:56 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5C9A985BA5; Mon, 20 Mar 2023 09:23:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=sberdevices.ru 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=sberdevices.ru header.i=@sberdevices.ru header.b="YudTLvCP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B4AE885C4D; Mon, 20 Mar 2023 09:23:42 +0100 (CET) Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 412B885B6E for ; Mon, 20 Mar 2023 09:23:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=sberdevices.ru Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=EABachinin@sberdevices.ru Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 5A68B5FD0B; Mon, 20 Mar 2023 11:23:35 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1679300615; bh=mfNfA51viHFzh7jPDqG+E8dTBJVoMnSijlE/lp4k33w=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=YudTLvCPWOOnGk2e5+SYuM03B3rVeWfOyF8PHB4IhGbWhHJ5D6fQiDs9hiFdB5Y4Y a5NVDxlnYkYqdrtsXchcFw3pJMsEudL57spNu/xhj4ePShD6keSOiBMXWAc2E78IR9 g605qHfRYSdo/jPFxTJGKDakyLIq1OTbzCDd1pi00eHMN+G0yUmUK0BTIjylRp7sRB HPNgYu8Py/siPF/PiEPeJfhh+KMtwPcbFowYzcmjdptxdxBveZua68m7zHP7VevHFl OhtR7vDTOBe/A22UFWt0i8ortQnrihqShNIbv/4wwAAo7XFDhnnAB4sn9d4+QUqvKS eShUT2ILGkJNA== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Mon, 20 Mar 2023 11:23:35 +0300 (MSK) From: Evgeny Bachinin To: Simon Glass , Hector Palacios , Marek Vasut , Heinrich Schuchardt , John Keeping CC: , , , Evgeny Bachinin Subject: [PATCH v2 1/4] cli: run_commandf(): small fixups Date: Mon, 20 Mar 2023 11:23:11 +0300 Message-ID: <20230320082314.2018-2-EABachinin@sberdevices.ru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230320082314.2018-1-EABachinin@sberdevices.ru> References: <20230320082314.2018-1-EABachinin@sberdevices.ru> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [172.16.1.6] X-ClientProxiedBy: S-MS-EXCH01.sberdevices.ru (172.16.1.4) To S-MS-EXCH01.sberdevices.ru (172.16.1.4) X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2023/03/20 04:57:00 #20976224 X-KSMG-AntiVirus-Status: Clean, skipped 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 * vsnprintf() can truncate cmd, hence it makes no sense to launch such command (it's broken). Moreover, it's better to signalize to the caller about such case (for facilitating debugging or bug hunting). * Fix kernel-doc warnings: include/command.h:264: info: Scanning doc for run_commandf include/command.h:268: warning: contents before sections include/command.h:271: warning: No description found for return value of 'run_commandf' * Add printf-like format attribute to validate at compile-time the format string against parameters's type. * Fix compilation error in case of -Wall, -Werror, -Wextra: error: variable ā€˜i’ set but not used [-Werror=unused-but-set-variable] * Drop extra ret variable. Signed-off-by: Evgeny Bachinin --- Changes for v2: - s/pr_err/pr_debug/ to reduce code size for rare errors - s/EINVAL/ENOSPC/ - replace on-stack buffer with global console_buffer[] - use not full (CONFIG_SYS_CBSIZE + 1) space of console_buffer, because interpreters return no more than CONFIG_SYS_CBSIZE (including \0), hence we use CONFIG_SYS_CBSIZE as a max command size for run_commandf() - not apply Reviewed-by due to changes above common/cli.c | 25 +++++++++++++++++++------ include/command.h | 13 ++++++++++--- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/common/cli.c b/common/cli.c index 9451e6a142..3916a7b10a 100644 --- a/common/cli.c +++ b/common/cli.c @@ -8,6 +8,8 @@ * JinHua Luo, GuangDong Linux Center, */ +#define pr_fmt(fmt) "cli: %s: " fmt, __func__ + #include #include #include @@ -20,6 +22,7 @@ #include #include #include +#include #ifdef CONFIG_CMDLINE /* @@ -129,16 +132,26 @@ int run_command_list(const char *cmd, int len, int flag) int run_commandf(const char *fmt, ...) { va_list args; - char cmd[128]; - int i, ret; + int nbytes; va_start(args, fmt); - i = vsnprintf(cmd, sizeof(cmd), fmt, args); + /* + * Limit the console_buffer space being used to CONFIG_SYS_CBSIZE, + * because its last byte is used to fit the replacement of \0 by \n\0 + * in underlying hush parser + */ + nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args); va_end(args); - ret = run_command(cmd, 0); - - return ret; + if (nbytes < 0) { + pr_debug("I/O internal error occurred.\n"); + return -EIO; + } else if (nbytes >= CONFIG_SYS_CBSIZE) { + pr_debug("'fmt' size:%d exceeds the limit(%d)\n", + nbytes, CONFIG_SYS_CBSIZE); + return -ENOSPC; + } + return run_command(console_buffer, 0); } /****************************************************************************/ diff --git a/include/command.h b/include/command.h index 0db4898062..0e153c6046 100644 --- a/include/command.h +++ b/include/command.h @@ -13,6 +13,8 @@ #include #include +#include + #ifndef NULL #define NULL 0 #endif @@ -260,12 +262,17 @@ int run_command_repeatable(const char *cmd, int flag); /** * run_commandf() - Run a command created by a format string * - * The command cannot be larger than 127 characters - * * @fmt: printf() format string * @...: Arguments to use (flag is always 0) + * + * The command cannot be larger than (CONFIG_SYS_CBSIZE - 1) characters. + * + * Return: + * Returns 0 on success, -EIO if internal output error occurred, -ENOSPC in + * case of 'fmt' string truncation, or != 0 on error, specific for + * run_command(). */ -int run_commandf(const char *fmt, ...); +int run_commandf(const char *fmt, ...) __printf(1, 2); /** * Run a list of commands separated by ; or even \0 -- 2.17.1