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=-4.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 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 2B2C2C11F65 for ; Wed, 30 Jun 2021 16:17:57 +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 3BE3561463 for ; Wed, 30 Jun 2021 16:17:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3BE3561463 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com 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 B510682BEA; Wed, 30 Jun 2021 18:17:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.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=gmail.com header.i=@gmail.com header.b="SHATf/vQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0AD2882C0B; Wed, 30 Jun 2021 18:17:53 +0200 (CEST) Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4640B82BEA for ; Wed, 30 Jun 2021 18:17:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qk1-x72b.google.com with SMTP id 19so2960054qky.13 for ; Wed, 30 Jun 2021 09:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=aNT+dEl88X6+faqK5IF0z3edpSCleMqLSASoQ6A5dxQ=; b=SHATf/vQnvZoBTKx/wzZlClB7qVIBOf/F4k6snH13Wu54IQlL6LtT8ldEm0ytccglg m95PIronDv9123/JxJ3k7pXLKwnLVZBOFyqghHWjQdhCMjEbj8dKfiv17SpSaC89tZ1Y cq+AIOrNq/7N1IaADTscGjDT5LVNS/DhAUWBUVPeofXG/jicbF4Jud3QdzynvtSumfGK KZtHZpnowrY1MM/U6iRxbo9HfMNCh9BHGwMi8i1gO5+/5IR9Z3NR3zeoqBCPgwX9/vpx 2JAnaRO/IF0xIxFutPzt/cEuV2rwIaesZhcvvQbPfeQsVfUT6f7E90lDXDO5dTVmT6kO adog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=aNT+dEl88X6+faqK5IF0z3edpSCleMqLSASoQ6A5dxQ=; b=NKw3s8KlvBNg36OcuscxZN0QBmyZDA7tspBn65nsL/AORb6AFkHhh8+6IVKDp8baIr LJ+nO/IxFrJjNjgh99CoiegVovuitg/ehzbwBF3XtBdr64LkoF5634yKPuk+3SG12AzD aHewWH43dtkwG/mI41YGurDuBp+k5OukiKGKFkWrEnlTrMzin+Fj0m/8fbK9iciFOSzy syizCp9foxE+svPGAf6amfRPJnc7LB69vhbxA3ZrmF8bki7J3lzrZ2fFkAhk9POYzjVE LXbrlHKejMBuPJSaslDRhFwY384o9LqxxjmE8ZjpMAtf8v1KDxbTlqMAaOZj/YTQEh+2 gRNw== X-Gm-Message-State: AOAM530FAeQt9nrb0HMx1rgl5vEQRFnKKXGALWBRmPNVyibi1xYdju/F etPtAsHnI+tENeDcRX+JiZA= X-Google-Smtp-Source: ABdhPJzeHKeAv8S+fACqdw8EKI1qsDgTAlQYfQFzlMHmu+QV9G05OIixNlC9LMOZr3TDq/AMJCKsLg== X-Received: by 2002:a05:620a:29d4:: with SMTP id s20mr37400018qkp.287.1625069869016; Wed, 30 Jun 2021 09:17:49 -0700 (PDT) Received: from [192.168.1.201] (pool-74-96-87-9.washdc.fios.verizon.net. [74.96.87.9]) by smtp.googlemail.com with ESMTPSA id h12sm13263109qkj.52.2021.06.30.09.17.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Jun 2021 09:17:48 -0700 (PDT) Subject: Re: [PATCH 0/3] cmd: setexpr: add fmt format string operation To: Wolfgang Denk 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 References: <20210628151750.572837-1-roland.gaudig-oss@weidmueller.com> <83741.1624955845@gemini.denx.de> <19b6eeea-2aad-972b-aeeb-8959aab17d7a@gmail.com> <103545.1624979613@gemini.denx.de> From: Sean Anderson Message-ID: Date: Wed, 30 Jun 2021 12:17:47 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <103545.1624979613@gemini.denx.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit 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 On 6/29/21 11:13 AM, Wolfgang Denk wrote: > 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. snprintf(buf, sizeof(buf), argv[3], argc >= 4 ? argv[4] : NULL, argc >= 5 ? argv[5] : NULL, argc >= 6 ? argv[6] : NULL, /* etc */); and you keep doing this until you get to whatever number of arguments you'd like. > >> 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. It's not a "checking" problem. The issue is that "123" cannot be passed directly to %d. So you have dig into the guts of snprintf anyway. --Sean