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.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, 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 5CDC9C11F65 for ; Wed, 30 Jun 2021 08:30:36 +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 6F61561CC1 for ; Wed, 30 Jun 2021 08:30:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6F61561CC1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=weidmueller.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 D64E083249; Wed, 30 Jun 2021 10:30:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=weidmueller.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=weidmueller.onmicrosoft.com header.i=@weidmueller.onmicrosoft.com header.b="SIuyH0mb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A173A83270; Wed, 30 Jun 2021 10:30:30 +0200 (CEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01on060a.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe1f::60a]) (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 6690182BD8 for ; Wed, 30 Jun 2021 10:30:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=weidmueller.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=roland.gaudig-oss@weidmueller.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aRDlyfKHgMY5fID2Fdw2XTTFZlpWbMBwiYeurYQmDp64qdlpj7b7fBLOngfirFjr3V04HCIGU2m3y+Pw1qvdaUhEJFC7dBMMk9QUe1mlButR5Z9QWVvW7Ze1feIGnJZ9yWYuuvczNWUSWOtjSKpFdjwlONvJ0PUp2CPY3BvnszOD3nUitgJv6boNGgf5bXsZNjp8hT4hsMYvUX5RI6d/pMXXIPErth5KET6ohT9wPtiMp3beh+GyOq5ECyCTVYZ4M0ZF9vhysM7xifDB4T6luR4tJuf+GndEiz5ikEh0qK69SP3pWWA1OTChHy5Y1l16aR2eguJSjcTjvPD2Po7NvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VbIMTLBsKedkYXFIJwxejaQN3k+1AN/s7Tbkl7KhdKM=; b=f0o8ORv6XVihTVa37sgx+No06kDVd5/kw1lY085uth+Nq8FeWbMyNFbLZv+EK0NLJDsMtKXKBEhYux52xKNULqFBepixQTpPRd9dQSE4oBqmQpou05+nk2t1oUg7HWlK7D6dx8r5roDnhdN19RnhinZGys9y/5QZ2hWiXiD6onrc4k0p6hS2spFfk/6FZr8RuvXDoQB3160wHQPoCTrwCE92NVyE/4AWi5ZxV0TZEdRR7zi9pg12uftSh/tsDLX1paNu1XSpk/gSDU6VyyDwIYtOSkhIGGAH6BlYUCfadPBZhA4/KOJ2QSIlb9A85EAx/0+LcW462IDZ5wSQbGsMtg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=weidmueller.com; dmarc=pass action=none header.from=weidmueller.com; dkim=pass header.d=weidmueller.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=weidmueller.onmicrosoft.com; s=selector1-weidmueller-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VbIMTLBsKedkYXFIJwxejaQN3k+1AN/s7Tbkl7KhdKM=; b=SIuyH0mbpC0MsrpU3QeaA9tJu/FSrUj2Mijw9V06kyzRaOnY6eL0Zswm0xxiAtMxtttb3meJHQZqJXTBfhc5imKHSNyDa6ZH8/ora1iNU9zk6Wh8KVUgQppxnvWDdvZkxZYiojD7ayqfVTdQ73N6X4vOV86oFYwZZnhL0huNB9s= Authentication-Results: mediatek.com; dkim=none (message not signed) header.d=none;mediatek.com; dmarc=none action=none header.from=weidmueller.com; Received: from DB9PR08MB6729.eurprd08.prod.outlook.com (2603:10a6:10:2ac::15) by DB7PR08MB2985.eurprd08.prod.outlook.com (2603:10a6:5:20::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4242.21; Wed, 30 Jun 2021 08:30:25 +0000 Received: from DB9PR08MB6729.eurprd08.prod.outlook.com ([fe80::e95e:a0e7:1d27:2496]) by DB9PR08MB6729.eurprd08.prod.outlook.com ([fe80::e95e:a0e7:1d27:2496%7]) with mapi id 15.20.4287.021; Wed, 30 Jun 2021 08:30:25 +0000 Subject: Re: [PATCH 0/3] cmd: setexpr: add fmt format string operation To: Wolfgang Denk Cc: 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 , Sean Anderson , Stefan Bosch , Weijie Gao References: <20210628151750.572837-1-roland.gaudig-oss@weidmueller.com> <83741.1624955845@gemini.denx.de> <89618.1624963251@gemini.denx.de> From: "Roland Gaudig (OSS)" Message-ID: <13f63a2d-badc-b79b-7cc4-9c8acc65b2c7@weidmueller.com> Date: Wed, 30 Jun 2021 08:30:23 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 In-Reply-To: <89618.1624963251@gemini.denx.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Originating-IP: [84.146.32.198] X-ClientProxiedBy: PR3P191CA0039.EURP191.PROD.OUTLOOK.COM (2603:10a6:102:55::14) To DB9PR08MB6729.eurprd08.prod.outlook.com (2603:10a6:10:2ac::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from DE12086.weidmueller.com (84.146.32.198) by PR3P191CA0039.EURP191.PROD.OUTLOOK.COM (2603:10a6:102:55::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.22 via Frontend Transport; Wed, 30 Jun 2021 08:30:24 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 80b5acd9-5f1a-4240-bd3c-08d93ba14f31 X-MS-TrafficTypeDiagnostic: DB7PR08MB2985: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8SmVyKd+vCsqwdJE/WeoJWmG85gPHT5D4+LGPvRE/0FEmgX+E4HmhmQ3kbqsQCE1Zszaf2fHK5qrsho1IJqx9GwCK8BsOaSqqutB3nVQhLPu9s9szonJHprOgT92PXPhoNw+AvbUkBOzwJzaMdmhla4O70yqTkvj6gWgDVcHMuL0TFNSdHPNVzrkf0IvkzwxH1JNbqOO70WQC0DymMXTJQgRttbVlWZHoZtUSzAQsIQTLoqneZzulJ4owDAPT79edbyyiNwllcZQGI/jAhguG7NS62/KA+pqOuHivdkL6I6MmhR5pyg1Ffbm47qSAFdVZQMl60pHyWyTFnCC+esz7lYZQxqyq2uTyc1zA+p7nkFUQQ2/Izsk90ZG2pO7TTvLcWYuZ7EDcT4lYbABZcggbUvNVqZIx3QLFt73gSlLOdRL5I8DpTCi2UEOUw3yIRfEtIRqkZDvj/h+S97zPV5BMcuaPpPhLDaEoVb6udx5VWmoRbl6/MnU0O8uFl3PZFP6pfs4s+VJYiYkOvO6deqxsFF4wiwzvESvfOPP2k/+izj4Z0LGHHV9U1shPd6WUd4t3KYQItjUm3Op9wbSXkC5ticf3z3dUfmGFH4WADqkLdGKT+4i4gOQd8fuwxYfMWl2OoildNRwrbZo7kx0zIch5jVrf0S3xYCz6cgUde7dQ9ZOxgCj18zAIgkGmXyxxS2Ct1VMWEcvTmU58dhlH72MQisvtjqtC4Qw1Js6REfXyOw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR08MB6729.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(366004)(136003)(376002)(39860400002)(346002)(5660300002)(956004)(66946007)(38100700002)(66476007)(53546011)(6916009)(86362001)(2616005)(66556008)(31696002)(54906003)(6486002)(316002)(478600001)(8676002)(83380400001)(4326008)(26005)(7416002)(31686004)(36756003)(7696005)(186003)(16526019)(8936002)(2906002)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?NnFEREJzQlVQYXcwNU1iMFI5bk96UW9rRHE0NnNoYVpMR3lwcnZLR0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-Network-Message-Id: 80b5acd9-5f1a-4240-bd3c-08d93ba14f31 X-MS-Exchange-CrossTenant-AuthSource: DB9PR08MB6729.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2021 08:30:25.5642 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: e4289438-1c5f-4c95-a51a-ee553b8b18ec X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: sRv4rMNv+I3mINRDLU8Mf2g6c6lESNQcgy9AJ1Zi/ddcfQVFNFhj/jZ1UOVhrpvTN6IwLUUiUxl67wUlsW1srQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR08MB2985 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 Hello Wolfgang On 29.06.21 10:40, Wolfgang Denk wrote: > > Dear Roland, > > In message you wrote: >> >>> These are two pretty unfortunate restrictions. I guess it should >>> not be too hard to avoid both of these. Can you please give it a >>> try? >> >> I think it is possible to allow more than one format parameter or more >> types. But it would make checking much more difficult. > > Maybe we need _less_ checking, not more - and maybe the needed > checking is already done in the *printf() code? The problem printf does not do much checking. For example in case the format string does not match the number of arguments or argument types in best case it just delivers a wrong result, but the program also can just crash. That is why I added the checks. In contrast Bash and Busybox are reporting error messages in the above cases. >> I think just passing the format string directly to sprintf should be >> avoided because it is unsafe. For example >> >> => setexpr foo fmt %s 0xffffffff >> >> would surely lead to access on memory location outside the variable >> where 0xffffffff is stored. > > Only if you make the wrong assumptions. I would expect this to > result in > > foo=0xffffffff > > in the same way as the bash builting gives > > $ printf '%s\n' 0xffffffff > 0xffffffff Yes, but that requires further checks and interpretation. To maintain the possibility to use pointers as arguments, the get_arg() function is necessary, but in the above example it would return a ulong which needs to be converted to a string before passing to printf, to get the above result. >>> => setexpr foo fmt "%0x08x-%s-%d-%s" $a $b $c $d >> >> I think the only way to support such expressions in a save way would >> be implementing an own format string parser for setexpr with > > Maybe it makes sense to have a look at the bash code? I looked at Bash code but it is quite confusing as they implement at least three format string parsers. I think the one relevant for us is the function printf_builtin() inside builtins/printf.dev. It has a length of about 450 lines. I also had a look at the busybox shell. They implemented their own format string parser too, which is also about 450 lines long. I don't see a leaner way for implementing a Bash like printf functionality with multiple arguments and all kinds of supported format types. When adding that format string capabilities in my opinion it should be a configuration option to keep code size low on systems not needing that functionality. Also I would tend to make the specific format string features configurable. For example in my application only decimal conversion is needed, also enabling floating point support would just increase code without bringing any benefits. Best regards, Roland Gaudig