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 X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BAB5C11F67 for ; Tue, 29 Jun 2021 15:13:48 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 1E00961DAD for ; Tue, 29 Jun 2021 15:13:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1E00961DAD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 25906831A3; Tue, 29 Jun 2021 17:13:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1624979625; bh=r1JKU4zX3sSZlYIpFSy/Kg7gbUItin3p2jO6epZxqaU=; h=To:cc:From:Subject:In-reply-to:References:Date:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=aqWuuRIpW3szQN3x0H3JYvgsTJlk5X9xOmRfMHsm9BKgu1CMpV4d0FbinTu4fDu56 CZqFqLOXM6OGK/R/5SiXWvNpVAdLLMdZ312MuECOfKdnqsGxMtPfoqqCGqj/8abS9D T7/ZKfCdz383LEn9GmCQphyXFV9oPNi0RMn05ALQfsxQ3ib2JMSZR80a85+y2uI4Sd GWVmDksOnXD7e9z2CLpesomXS5IACaAdZCcEalO5nTNCn21aDuV043F1g6owZmmNzl RZlZeiqPOKKvTlndDD10yHKebSU+u/Zw/sz0KI53ZkUIygk1yw2IMkqlxlell8Or9N i5SPyVhvO7/rQ== Received: from janitor.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: noc@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 585088315B for ; Tue, 29 Jun 2021 17:13:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1624979623; bh=r1JKU4zX3sSZlYIpFSy/Kg7gbUItin3p2jO6epZxqaU=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=fdSMuPVPvRkK9xEL3OlE9m8Z2t1C5ZNi+iQolNlUS3muPfxOYCYEM+j0+x5KkKjVr K5Ld0nxWxah1leHWc4d5UwCvmr4ZcPVS+3n/DPL+sXqQUOGuPeXWA2SbrNw1NcSZbr qOTIAyXrDp4K5TyCn3bmvyQU7YMuWcoUb4vD/uvWxso8h3YWk0o29GIWV7PwgGesMC WWEKDxCmeHBKxsVGLw1ApWhSHcS3IdGYpnahSWmOktn5CVgbHiqrLZ+l+VZ5+4zWoT qHbC7vuE85gV9vv2peoNqy/DkDOTNxJ9yAbQA1z5/KyiAiPT7f8MQWPkce4SB9/afO 3ySN8yXqECdbA== Received: by janitor.denx.de (Postfix, from userid 108) id 102ACA0250; Tue, 29 Jun 2021 17:13:43 +0200 (CEST) Received: from gemini.denx.de (gemini.denx.de [10.4.0.2]) by janitor.denx.de (Postfix) with ESMTPS id DFD20A009B; Tue, 29 Jun 2021 17:13:33 +0200 (CEST) Received: from gemini.denx.de (localhost [IPv6:::1]) by gemini.denx.de (Postfix) with ESMTP id 8C7911E1DD2; Tue, 29 Jun 2021 17:13:33 +0200 (CEST) To: Sean Anderson cc: Roland Gaudig , u-boot@lists.denx.de, Simon Glass , Roland Gaudig , Alex Nemirovsky , Bin Meng , Heinrich Schuchardt , =?UTF-8?Q?Marek_Beh=c3=ban?= , Patrick Delaunay , Rayagonda Kokatanur , Robert Marko , Stefan Bosch , Weijie Gao From: Wolfgang Denk Subject: Re: [PATCH 0/3] cmd: setexpr: add fmt format string operation MIME-Version: 1.0 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 8bit In-reply-to: <19b6eeea-2aad-972b-aeeb-8959aab17d7a@gmail.com> References: <20210628151750.572837-1-roland.gaudig-oss@weidmueller.com> <83741.1624955845@gemini.denx.de> <19b6eeea-2aad-972b-aeeb-8959aab17d7a@gmail.com> Comments: In-reply-to Sean Anderson message dated "Tue, 29 Jun 2021 09:57:19 -0400." Date: Tue, 29 Jun 2021 17:13:33 +0200 Message-ID: <103545.1624979613@gemini.denx.de> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean Dear Sean, In message <19b6eeea-2aad-972b-aeeb-8959aab17d7a@gmail.com> you wrote: > > The issue with this is twofold. First, there is no portable way to > construct a va_list from C code. So the likely way to do this would be > to set an arbitrary limit, and then just pass the arguments in. E.g. > something like We already have an argument list: it's what's being passed to the "setexpr" command, minus the initial arguments. > snprintf(buf, sizeof(buf), argv[3], argc >= 4 ? argv[4] : NULL, /* etc */); Why this test on argc? If it's less than 4, argv[4] should be NULL anyway. > but of course there is no way to check that the format string matches > the correct number of arguments. This is a pretty big footgun. You have this problem always when you have user provided format strings and arguments. We don't have to re-invent the wheel here. I repeat myself: maybe we should have a look at bash's implementation of the printf builtin command? there I get for example this: $ printf "%d %d %d\n" 3 3 0 0 $ printf "%d %d %d\n" foo bar -bash: printf: foo: invalid number -bash: printf: bar: invalid number 0 0 0 > The other problem is that things like `%d` expect a number and not a > string. So you would have to reimplement snprintf anyway so that it > expects all of its arguments to be strings, and calls strtoul as > appropriate. And considering that the *printf functions take 5k > already, this reimplementation may add a significant amount of code. > For this reason, I'd much prefer to just have `hex` and `dec` functions > which do the appropriate conversions. Eventually the format checking can be kept out of the generic *printf() code; it could then be optional/configurable with the "fmt" option in the setexpr command. Best regards, Wolfgang Denk -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction which doesn't work.