From patchwork Sun Nov 1 21:15:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391860 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=UELD31xf; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTPW1ngZz9sVK for ; Mon, 2 Nov 2020 08:16:31 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D198282545; Sun, 1 Nov 2020 22:16:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="UELD31xf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A30BD82544; Sun, 1 Nov 2020 22:15:59 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x142.google.com (mail-il1-x142.google.com [IPv6:2607:f8b0:4864:20::142]) (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 EDE378251C for ; Sun, 1 Nov 2020 22:15:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x142.google.com with SMTP id x7so11240089ili.5 for ; Sun, 01 Nov 2020 13:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1sGAqZ6aMRlU/0p3GF2Zt3IWqS2GjLO4gLfJSbZ/Jc0=; b=UELD31xfcRHtUIy/g7kOzaCsWPhGNqukp9RIqz1QHtojLcAAQ2DHGmAugkLfzOdT45 lLGiVLMVIgqZsMvCwH+MHlJm+pPqR4PuZYmvvLuNPmxl0RdnqMhuPLuITOCIwrCx6+aU PGTWkLLnlASXmSsDffkaUzB9bsUIslWQISkoQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1sGAqZ6aMRlU/0p3GF2Zt3IWqS2GjLO4gLfJSbZ/Jc0=; b=FXYzD1K6neuirNIf89S4vdSRxldRBrDLjcWgYaL32vqlDXCGKrXg/o3wgSGspFlFY+ anz/2IMRs0QuKLqn9oc1Tm9555+lfeVbsMIFqHnJxrbcn1/6wTD1SqIiJ9FX1gjlef/k GjYticms28zSfFaibmfP09AMU1m4c6woAPm41b78KkQd/Lm8eOdGUj5jfAIJy+bXRnXR 45c8+DcdraqzMQSuyYTZ+UYB0HGAbF1cxBDFxi0Gu7quww1pev12KDSMOqpSVaW8fVqn N7uzdZvnn97WwxffbgvEKVmyUkjPmwSjG8ShQxPHC9sA8uLj4YNoJe/Me3xehOkVcGKt ZPFw== X-Gm-Message-State: AOAM531YlvBjZhS9TBbmSHkEryy6r6skouCfdhnwU0hagD5RwW4kkkGn bNnQv7Oy5Gi7J3bIEI0kGx6f5g1HLWbd45/V X-Google-Smtp-Source: ABdhPJy84yX7XA+PUq7zsAtsFSBE6wyg0mMOdDzlQXzBxr8MnZHISXhSPGD/MpGPVsmDsB+dx0Xufw== X-Received: by 2002:a92:b003:: with SMTP id x3mr8859163ilh.4.1604265353464; Sun, 01 Nov 2020 13:15:53 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:52 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass , Heiko Stuebner , Heinrich Schuchardt , Philippe Reynes Subject: [PATCH 01/10] test: Add some tests for setexpr Date: Sun, 1 Nov 2020 14:15:35 -0700 Message-Id: <20201101211544.3579850-2-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean This command currently has no tests. Add some for basic assignment and the integer operations. Note that the default size for setexpr is ulong, which varies depending on the build machine. So for sandbox on a 64-bit host, this means that the default size is 64 bits. Signed-off-by: Simon Glass --- include/test/suites.h | 2 + test/cmd/Makefile | 1 + test/cmd/setexpr.c | 162 ++++++++++++++++++++++++++++++++++++++++++ test/cmd_ut.c | 3 + 4 files changed, 168 insertions(+) create mode 100644 test/cmd/setexpr.c diff --git a/include/test/suites.h b/include/test/suites.h index ab7b3bd9cad..5c97846e7f5 100644 --- a/include/test/suites.h +++ b/include/test/suites.h @@ -38,6 +38,8 @@ int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]); int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, diff --git a/test/cmd/Makefile b/test/cmd/Makefile index 859dcda2393..b2f71af8473 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -4,3 +4,4 @@ obj-y += mem.o obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o +obj-y += setexpr.o diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c new file mode 100644 index 00000000000..cab6fdf4b83 --- /dev/null +++ b/test/cmd/setexpr.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Tests for setexpr command + * + * Copyright 2020 Google LLC + * Written by Simon Glass + */ + +#include +#include +#include +#include +#include +#include + +#define BUF_SIZE 0x100 + +/* Declare a new mem test */ +#define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test) + +/* Test 'setexpr' command with simply setting integers */ +static int setexpr_test_int(struct unit_test_state *uts) +{ + u8 *buf; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\xff', BUF_SIZE); + + /* byte */ + buf[0x0] = 0x12; + ut_assertok(run_command("setexpr.b fred 0", 0)); + ut_asserteq_str("0", env_get("fred")); + ut_assertok(run_command("setexpr.b fred *0", 0)); + ut_asserteq_str("12", env_get("fred")); + + /* 16-bit */ + *(short *)buf = 0x2345; + ut_assertok(run_command("setexpr.w fred 0", 0)); + ut_asserteq_str("0", env_get("fred")); + ut_assertok(run_command("setexpr.w fred *0", 0)); + ut_asserteq_str("2345", env_get("fred")); + + /* 32-bit */ + *(u32 *)buf = 0x3456789a; + ut_assertok(run_command("setexpr.l fred 0", 0)); + ut_asserteq_str("0", env_get("fred")); + ut_assertok(run_command("setexpr.l fred *0", 0)); + ut_asserteq_str("ffffffff3456789a", env_get("fred")); + + /* 64-bit */ + *(u64 *)buf = 0x456789abcdef0123; + ut_assertok(run_command("setexpr.q fred 0", 0)); + ut_asserteq_str("0", env_get("fred")); + ut_assertok(run_command("setexpr.q fred *0", 0)); + ut_asserteq_str("456789abcdef0123", env_get("fred")); + + /* default */ + ut_assertok(run_command("setexpr fred 0", 0)); + ut_asserteq_str("0", env_get("fred")); + ut_assertok(run_command("setexpr fred *0", 0)); + ut_asserteq_str("456789abcdef0123", env_get("fred")); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC); + +/* Test 'setexpr' command with + operator */ +static int setexpr_test_plus(struct unit_test_state *uts) +{ + char *buf; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\xff', BUF_SIZE); + + /* byte */ + buf[0x0] = 0x12; + buf[0x10] = 0x34; + ut_assertok(run_command("setexpr.b fred *0 + *10", 0)); + ut_asserteq_str("46", env_get("fred")); + + /* 16-bit */ + *(short *)buf = 0x2345; + *(short *)(buf + 0x10) = 0xf012; + ut_assertok(run_command("setexpr.w fred *0 + *10", 0)); + ut_asserteq_str("11357", env_get("fred")); + + /* 32-bit */ + *(u32 *)buf = 0x3456789a; + *(u32 *)(buf + 0x10) = 0xc3384235; + ut_assertok(run_command("setexpr.l fred *0 + *10", 0)); + ut_asserteq_str("fffffffef78ebacf", env_get("fred")); + + /* 64-bit */ + *(u64 *)buf = 0x456789abcdef0123; + *(u64 *)(buf + 0x10) = 0x4987328372849283; + ut_assertok(run_command("setexpr.q fred *0 + *10", 0)); + ut_asserteq_str("8eeebc2f407393a6", env_get("fred")); + + /* default */ + ut_assertok(run_command("setexpr fred *0 + *10", 0)); + ut_asserteq_str("8eeebc2f407393a6", env_get("fred")); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC); + +/* Test 'setexpr' command with other operators */ +static int setexpr_test_oper(struct unit_test_state *uts) +{ + char *buf; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\xff', BUF_SIZE); + + *(u32 *)buf = 0x1234; + *(u32 *)(buf + 0x10) = 0x560000; + + /* Quote | to avoid confusing hush */ + ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0)); + ut_asserteq_str("ffffffff00561234", env_get("fred")); + + *(u32 *)buf = 0x561200; + *(u32 *)(buf + 0x10) = 0x1234; + + /* Quote & to avoid confusing hush */ + ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0)); + ut_asserteq_str("ffffffff00001200", env_get("fred")); + + ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0)); + ut_asserteq_str("560034", env_get("fred")); + + ut_assertok(run_command("setexpr.l fred *0 - *10", 0)); + ut_asserteq_str("55ffcc", env_get("fred")); + + ut_assertok(run_command("setexpr.l fred *0 * *10", 0)); + ut_asserteq_str("ffa9dbd21ebfa800", env_get("fred")); + + ut_assertok(run_command("setexpr.l fred *0 / *10", 0)); + ut_asserteq_str("1", env_get("fred")); + + ut_assertok(run_command("setexpr.l fred *0 % *10", 0)); + ut_asserteq_str("55ffcc", env_get("fred")); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC); + +int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + struct unit_test *tests = ll_entry_start(struct unit_test, + setexpr_test); + const int n_ents = ll_entry_count(struct unit_test, setexpr_test); + + return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc, + argv); +} diff --git a/test/cmd_ut.c b/test/cmd_ut.c index 8f0bc688a22..f79109e6f8e 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -75,6 +75,8 @@ static struct cmd_tbl cmd_ut_sub[] = { U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""), #endif U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""), + U_BOOT_CMD_MKENT(setexpr, CONFIG_SYS_MAXARGS, 1, do_ut_setexpr, "", + ""), #ifdef CONFIG_UT_TIME U_BOOT_CMD_MKENT(time, CONFIG_SYS_MAXARGS, 1, do_ut_time, "", ""), #endif @@ -153,6 +155,7 @@ static char ut_help_text[] = #ifdef CONFIG_UT_OVERLAY "ut overlay [test-name]\n" #endif + "ut setexpr [test-name] - test setexpr command\n" #ifdef CONFIG_SANDBOX "ut str - Basic test of string functions\n" #endif From patchwork Sun Nov 1 21:15:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391862 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=gR+EIT4s; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTPq43g6z9sRR for ; Mon, 2 Nov 2020 08:16:47 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B35CF82556; Sun, 1 Nov 2020 22:16:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="gR+EIT4s"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CCC778254B; Sun, 1 Nov 2020 22:16:01 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x144.google.com (mail-il1-x144.google.com [IPv6:2607:f8b0:4864:20::144]) (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 CF1CD82521 for ; Sun, 1 Nov 2020 22:15:55 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x144.google.com with SMTP id z2so11176982ilh.11 for ; Sun, 01 Nov 2020 13:15:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OTuhF2pZ+W2Vr59mvJuJUadoy1apk8/29jPNCHJRD1k=; b=gR+EIT4s0x1ZHOlkV6GGwAtawsReDWtz6HiaNcg+F7Gk7akxaLlo6DdkUtR/2MVXbV g1Zwb/vsEXS/cTG0ll/cUNEZLa7JY6ZhXqgb2sbFu4KnEOTCKu2VyB9/Qj5OT7+ntLJ5 ykiFF3X9HED1YjTbvClqWCZNhSVlvF5QwyxYY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OTuhF2pZ+W2Vr59mvJuJUadoy1apk8/29jPNCHJRD1k=; b=HXSLu1xkM3xY9ID9JuUy39kfcwaM46O/VVcCeopnBQr0OJhAvM4o9O2P/wNL62S41A 9GQKAoseZ5cJJmWnHkMIYwCaTz4bcO/eqvStl/t/tY5DZDrm4ImhdApSH7YZ0cAPC/Gr YOjdM+KMlWCn0g4TYe+/bbfjvzeyeDhS83xhr2KS86+qiJ/hECRs/aqNahMifvHTQQv+ jKuWjK3+i4hcCMr8m6ookLsSD5vxaVfNEhjrroj/tjEcC5gcFbjqhs13LBi7TxcSnTgF UW+HLO6LLIoQtQzQWKmhbzv0tcnDsG2poC0A52cNJ6+30pSA/FrgS4jsoMBnqlz0i7gJ z6Rg== X-Gm-Message-State: AOAM533J3N5Uf8dkalZ9nHDAk5o4B3Ltm2wP7F0VIQudxSmHesL3tW8W b2rRGUo5b4EqVo4+/gacWynJaA/qeY0ObzUA X-Google-Smtp-Source: ABdhPJznC7Pg2NQY8ocwXhaVtjCVoqL6TN23MbSKhIWT46LIN4BGAvK7Ov2kicG7tRuIYs6ZPey2+w== X-Received: by 2002:a92:6410:: with SMTP id y16mr8156049ilb.126.1604265354529; Sun, 01 Nov 2020 13:15:54 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:54 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass , Michal Simek , Stefan Roese Subject: [PATCH 02/10] command: Add constants for cmd_get_data_size string / error Date: Sun, 1 Nov 2020 14:15:36 -0700 Message-Id: <20201101211544.3579850-3-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present these values are open-coded in a few places. Add constants so the meaning is clear. Also add a comment to cmd_get_data_size() Signed-off-by: Simon Glass --- cmd/itest.c | 4 ++-- cmd/mem.c | 2 +- common/command.c | 4 ++-- include/command.h | 26 +++++++++++++++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cmd/itest.c b/cmd/itest.c index a0cf4bee041..9a441ce9b8a 100644 --- a/cmd/itest.c +++ b/cmd/itest.c @@ -197,10 +197,10 @@ static int do_itest(struct cmd_tbl *cmdtp, int flag, int argc, #endif value = binary_test (argv[2], argv[1], argv[3], w); break; - case -2: + case CMD_DATA_SIZE_STR: value = binary_test (argv[2], argv[1], argv[3], 0); break; - case -1: + case CMD_DATA_SIZE_ERR: default: puts("Invalid data width specifier\n"); value = 0; diff --git a/cmd/mem.c b/cmd/mem.c index 56e1d0755b6..1d4f2bab2f9 100644 --- a/cmd/mem.c +++ b/cmd/mem.c @@ -393,7 +393,7 @@ static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc, * Defaults to long if no or incorrect specification. */ size = cmd_get_data_size(argv[0], 4); - if (size < 0 && size != -2 /* string */) + if (size < 0 && size != CMD_DATA_SIZE_STR) return 1; argc--; diff --git a/common/command.c b/common/command.c index 2c491e20a74..068cb55b4cd 100644 --- a/common/command.c +++ b/common/command.c @@ -475,13 +475,13 @@ int cmd_get_data_size(char* arg, int default_size) case 'l': return 4; case 's': - return -2; + return CMD_DATA_SIZE_STR; case 'q': if (MEM_SUPPORT_64BIT_DATA) return 8; /* no break */ default: - return -1; + return CMD_DATA_SIZE_ERR; } } return default_size; diff --git a/include/command.h b/include/command.h index b9b5ec1afa0..e900f97df33 100644 --- a/include/command.h +++ b/include/command.h @@ -117,7 +117,31 @@ int cmd_process_error(struct cmd_tbl *cmdtp, int err); defined(CONFIG_CMD_PCI) || \ defined(CONFIG_CMD_SETEXPR) #define CMD_DATA_SIZE -extern int cmd_get_data_size(char* arg, int default_size); +#define CMD_DATA_SIZE_ERR (-1) +#define CMD_DATA_SIZE_STR (-2) + +/** + * cmd_get_data_size() - Get the data-size specifier from a command + * + * This reads a '.x' size specifier appended to a command. For example 'md.b' + * is the 'md' command with a '.b' specifier, meaning that the command should + * use bytes. + * + * Valid characters are: + * + * b - byte + * w - word (16 bits) + * l - long (32 bits) + * q - quad (64 bits) + * s - string + * + * @arg: Pointers to the command to check. If a valid specifier is present it + * will be the last character of the string, following a '.' + * @default_size: Default size to return if there is no specifier + * @return data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid + * character, or CMD_DATA_SIZE_STR for a string + */ +int cmd_get_data_size(char *arg, int default_size); #endif #ifdef CONFIG_CMD_BOOTD From patchwork Sun Nov 1 21:15:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391865 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=Sn66MDr4; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTQV69W2z9sRR for ; Mon, 2 Nov 2020 08:17:22 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E722B82553; Sun, 1 Nov 2020 22:16:24 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="Sn66MDr4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 547DB82544; Sun, 1 Nov 2020 22:16:05 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x142.google.com (mail-il1-x142.google.com [IPv6:2607:f8b0:4864:20::142]) (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 C436382537 for ; Sun, 1 Nov 2020 22:15:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x142.google.com with SMTP id v18so11269181ilg.1 for ; Sun, 01 Nov 2020 13:15:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7tWqU0CevLJczm+b1H/GCgVG+zmv70Mz0zK5rxauNEk=; b=Sn66MDr4/ZG2y0K5o8rQJl/gY+tqVIkYLEGyqZ7XKgiginkIkJWGAOvn/7qTrX63uf AW+tFKQ9tQ6NOfC9WoBpOa3bWxOikBQyO0OlbzWaLeyP5+XRuwJTX1CLqVekiI5Hf3rl Mvungoh4qOk8fIbV54IVnhlR9KxBO1mEQCPTA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7tWqU0CevLJczm+b1H/GCgVG+zmv70Mz0zK5rxauNEk=; b=GBoxTmZtuSqVIp7jjcX17Lsshx0Z0G+YwgIGnXiurAUwexNMEh4ySrvR0+p/AB/5gJ rm2B5EvTXxDP/lZuuHhjtRgUOzLXbcr67LJqwvCoDo/+TpMLJe0PalMQ3gP4jRRdJ5sx wTK2oU5RVjaQsSlRwvXjjrj7SvTbava8UbTrJ4SeGxNyRD44OOg74n6yi5UDXszeq8PC GEmPjEbhgfsTfS9+2vQhSJIrTe8JVQPuXhkntsKQjgMYylYXaeDzGubvb3xH9CjyBiIo mGvNUu30wavH4eS5BmORAG4deSRd/mXWSF9VYvUpR94J0PDsS5dahi6KXKiW9UZJ6fNK RPYQ== X-Gm-Message-State: AOAM531VSugWQsZu7obGXOuyZhqmSIFz6WPO8xx/aCc+neJ5JnaseCTq eWsfhDaqLw9oYLD6mpr+QvtGyraahFdAveH3 X-Google-Smtp-Source: ABdhPJx/eQC0t36V/DpK3tzd9+bZAgA4GJUudHQ/6XmAd5PA9grhZtoiwb0Jq7R6JQR85YOkGvHQKA== X-Received: by 2002:a92:d2ca:: with SMTP id w10mr8496313ilg.140.1604265355487; Sun, 01 Nov 2020 13:15:55 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:54 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 03/10] setexpr: Add explicit support for 32- and 64-bit ints Date: Sun, 1 Nov 2020 14:15:37 -0700 Message-Id: <20201101211544.3579850-4-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present this function assumes that a size of 4 refers to a ulong. This is true on 32-bit machines but not commonly on 64-bit machines. This means that the 'l' specify does not work correctly with setexpr. Add an explicit case for 32-bit values so that 64-bit machines can still use the 'l' specifier. On 32-bit machines, 64-bit is still not supported. This corrects the operation of the default size (which is 4 for setexpr), so update the tests accordingly. The original code for reading from memory was included in 47ab5ad1457 ("cmd_setexpr: allow memory addresses in expressions") but I am not adding a Fixes: tag since that code was not written with 64-bit machines in mind. Signed-off-by: Simon Glass --- cmd/setexpr.c | 4 ++++ test/cmd/setexpr.c | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index 770dc24d2b7..dd9c2574fdc 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -39,6 +39,10 @@ static ulong get_arg(char *s, int w) unmap_sysmem(p); return val; case 4: + p = map_sysmem(addr, sizeof(u32)); + val = *(u32 *)p; + unmap_sysmem(p); + return val; default: p = map_sysmem(addr, sizeof(ulong)); val = *p; diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index cab6fdf4b83..e950c380ce0 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -45,7 +45,7 @@ static int setexpr_test_int(struct unit_test_state *uts) ut_assertok(run_command("setexpr.l fred 0", 0)); ut_asserteq_str("0", env_get("fred")); ut_assertok(run_command("setexpr.l fred *0", 0)); - ut_asserteq_str("ffffffff3456789a", env_get("fred")); + ut_asserteq_str("3456789a", env_get("fred")); /* 64-bit */ *(u64 *)buf = 0x456789abcdef0123; @@ -58,7 +58,7 @@ static int setexpr_test_int(struct unit_test_state *uts) ut_assertok(run_command("setexpr fred 0", 0)); ut_asserteq_str("0", env_get("fred")); ut_assertok(run_command("setexpr fred *0", 0)); - ut_asserteq_str("456789abcdef0123", env_get("fred")); + ut_asserteq_str("cdef0123", env_get("fred")); unmap_sysmem(buf); @@ -90,7 +90,7 @@ static int setexpr_test_plus(struct unit_test_state *uts) *(u32 *)buf = 0x3456789a; *(u32 *)(buf + 0x10) = 0xc3384235; ut_assertok(run_command("setexpr.l fred *0 + *10", 0)); - ut_asserteq_str("fffffffef78ebacf", env_get("fred")); + ut_asserteq_str("f78ebacf", env_get("fred")); /* 64-bit */ *(u64 *)buf = 0x456789abcdef0123; @@ -100,7 +100,7 @@ static int setexpr_test_plus(struct unit_test_state *uts) /* default */ ut_assertok(run_command("setexpr fred *0 + *10", 0)); - ut_asserteq_str("8eeebc2f407393a6", env_get("fred")); + ut_asserteq_str("1407393a6", env_get("fred")); unmap_sysmem(buf); @@ -121,14 +121,14 @@ static int setexpr_test_oper(struct unit_test_state *uts) /* Quote | to avoid confusing hush */ ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0)); - ut_asserteq_str("ffffffff00561234", env_get("fred")); + ut_asserteq_str("561234", env_get("fred")); *(u32 *)buf = 0x561200; *(u32 *)(buf + 0x10) = 0x1234; /* Quote & to avoid confusing hush */ ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0)); - ut_asserteq_str("ffffffff00001200", env_get("fred")); + ut_asserteq_str("1200", env_get("fred")); ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0)); ut_asserteq_str("560034", env_get("fred")); @@ -137,13 +137,13 @@ static int setexpr_test_oper(struct unit_test_state *uts) ut_asserteq_str("55ffcc", env_get("fred")); ut_assertok(run_command("setexpr.l fred *0 * *10", 0)); - ut_asserteq_str("ffa9dbd21ebfa800", env_get("fred")); + ut_asserteq_str("61ebfa800", env_get("fred")); ut_assertok(run_command("setexpr.l fred *0 / *10", 0)); - ut_asserteq_str("1", env_get("fred")); + ut_asserteq_str("4ba", env_get("fred")); ut_assertok(run_command("setexpr.l fred *0 % *10", 0)); - ut_asserteq_str("55ffcc", env_get("fred")); + ut_asserteq_str("838", env_get("fred")); unmap_sysmem(buf); From patchwork Sun Nov 1 21:15:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391863 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=br/2ZnjO; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTQ04tssz9sVK for ; Mon, 2 Nov 2020 08:16:56 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 206348255C; Sun, 1 Nov 2020 22:16:16 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="br/2ZnjO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7171782558; Sun, 1 Nov 2020 22:16:04 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) (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 C778B82517 for ; Sun, 1 Nov 2020 22:15:57 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd44.google.com with SMTP id n12so1327004ioc.2 for ; Sun, 01 Nov 2020 13:15:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=c0FH5e0Si19htIUh06PDsJuKYpg9750+rfVL6yA1w8A=; b=br/2ZnjOPnD/rzmDa+yky/CcAzv8U8qCs5VQwMfW6G1srmcJD85xcffphBziGlYAWM ePDFSXFt2mfWTkUPBDjhzw+3wwItbPq6HlCv4745mML3XChEf5ymKlZ15241pXCBCWAE 1mEFsLf6JlXFHrgZxQ+BytJY/cvNXpUJsK3SM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=c0FH5e0Si19htIUh06PDsJuKYpg9750+rfVL6yA1w8A=; b=lf8txUpnV3Hu24k0L39QkpZK3mv3vfrQLBkGU+zZRB0UwTLD3brnqpqTPj0Cx/cgPR 6H2+MFF8iXwnAEtf8xAIXab02HqQA0pk3BNIKWqMLeGZXTMvMa7vqpS8T1ULHPV7L7Q7 1vhFrBOkroktGYd/nhG2x/sDNfwsX8uuAfc0xozERLKD157lJdvGBOXvi+KO27qhXwmZ gJ2HvQ5wYZIhpsRL3GZ4kbVApc7fY4jPZNihjXUSPTEqeS2nqedxfueCRP5L7lE+94G5 fXZbP/CCIB7KAhOymHL0WkJ1mIh2avuBOOet3NBYqUVF+6CoKoyo3WlkP126FTCIozW1 W5Ow== X-Gm-Message-State: AOAM531u8TwCb0csOXZJtVxCtz43mjrfAzgo3cWtWUia8jgTvcae5Ne1 XY0gNMAY473iUJBxsL5AUb7Z76U4CI4k1M/K X-Google-Smtp-Source: ABdhPJwQV6onZ0hc3ojjB47DwaT2fCtJ8nWzH3nSYyKhOomYge0o5xZ13neXFK9XgdQPQyN4wZwqWg== X-Received: by 2002:a05:6602:164b:: with SMTP id y11mr1598831iow.36.1604265356454; Sun, 01 Nov 2020 13:15:56 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:55 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 04/10] test: Add some setexpr regex tests Date: Sun, 1 Nov 2020 14:15:38 -0700 Message-Id: <20201101211544.3579850-5-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean Add tests for the setexpr regex commands. Note that these tests currently crash on sandbox due to an existing bug in the setexpr implementation, so two of the tests are commented out. Signed-off-by: Simon Glass --- test/cmd/setexpr.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index e950c380ce0..de54561917c 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -151,6 +151,64 @@ static int setexpr_test_oper(struct unit_test_state *uts) } SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC); +/* Test 'setexpr' command with regex */ +static int setexpr_test_regex(struct unit_test_state *uts) +{ + char *buf, *val; + + buf = map_sysmem(0, BUF_SIZE); + + /* Single substitution */ + ut_assertok(run_command("setenv fred 'this is a test'", 0)); + ut_assertok(run_command("setexpr fred sub is us", 0)); + val = env_get("fred"); + ut_asserteq_str("thus is a test", val); + + /* Global substitution */ + ut_assertok(run_command("setenv fred 'this is a test'", 0)); + if (0) { + /* Causes a crash at present due to a bug in setexpr */ + ut_assertok(run_command("setexpr fred gsub is us", 0)); + val = env_get("fred"); + ut_asserteq_str("thus us a test", val); + } + /* Global substitution */ + ut_assertok(run_command("setenv fred 'this is a test'", 0)); + ut_assertok(run_command("setenv mary 'this is a test'", 0)); + ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0)); + val = env_get("fred"); + ut_asserteq_str("thus us a test", val); + val = env_get("mary"); + ut_asserteq_str("this is a test", val); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC); + +/* Test 'setexpr' command with regex replacement that expands the string */ +static int setexpr_test_regex_inc(struct unit_test_state *uts) +{ + char *buf, *val; + + buf = map_sysmem(0, BUF_SIZE); + + ut_assertok(run_command("setenv fred 'this is a test'", 0)); + if (0) { + /* Causes a crash at present due to a bug in setexpr */ + ut_assertok(run_command("setexpr fred gsub is much_longer_string", + 0)); + val = env_get("fred"); + ut_asserteq_str("thmuch_longer_string much_longer_string a test", + val); + } + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC); + int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = ll_entry_start(struct unit_test, From patchwork Sun Nov 1 21:15:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391864 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=h14C/kD9; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTQF4q8hz9sRR for ; Mon, 2 Nov 2020 08:17:09 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 68ABA82554; Sun, 1 Nov 2020 22:16:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="h14C/kD9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DF2628254B; Sun, 1 Nov 2020 22:16:06 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x141.google.com (mail-il1-x141.google.com [IPv6:2607:f8b0:4864:20::141]) (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 0530B8253B for ; Sun, 1 Nov 2020 22:15:59 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x141.google.com with SMTP id p10so11246097ile.3 for ; Sun, 01 Nov 2020 13:15:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yZUoCoXZ3Vf8fqoG9oRZBRwEvUanJ3GWUQcycaQNnz0=; b=h14C/kD94Q5bzCgjxJy3crL+WDu19zMKSVUFEZLk4wVtovO7GG6+seAGtjbEnCe4OR TRZ9F57HY1aVapNWwJ016N7F2czA3SOgmVpykg7c4DNl2qNxo78Mu93nhuaFsY3f2Kvh kx1Er4TOtLhmrmsGJeZhFmpU+bLVyFJzAgt1g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yZUoCoXZ3Vf8fqoG9oRZBRwEvUanJ3GWUQcycaQNnz0=; b=pDU04iPM2ZrPqQN6crsWUm1Jt33gCUsCXU9QqL1rufb/Vx4OcW0MOGVpYQZBKMJw/q /IDausSHI/19YfbljIc8WNal7cvQefDH/suG+nOwUfUVPSi1HGCVXmru5WV4YOgpD4ya J+wSwwK8W9YDKKDfavpImxWCf4DgdefaLjsixe+Q9pNzmm9GJepVdbA0Z8/fkTOPMcty CGFmQy6OTfw6QltODrhzJSpqxKZ/amRb2U/n6M6lzhqiAMZaBvckLxmnTZ2vKXvqTV8c SmxsgYcor1QyXxsRLi9GoZAbQINCDjwapWlFTzy/8JaA7+5VBC0EnZ3Hds3hT9pVOrvF mbag== X-Gm-Message-State: AOAM531/UucZp2pCooC9IG4l22Y7mrQI8SanBF5g2pu/39mrXFOLvW79 4N1pPddUMjyLXMqe4ZKLD6d79VHqF3lVz5BS X-Google-Smtp-Source: ABdhPJy7TuVFzkdvRSAyxnliwXUxqV8w72x6HW2CoMScq8FwtT7hWSYuDZtoG5LXeaLPdfMADB3yZg== X-Received: by 2002:a92:9a8c:: with SMTP id c12mr8550177ill.186.1604265357455; Sun, 01 Nov 2020 13:15:57 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:56 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 05/10] setexpr: Split the core logic into its own function Date: Sun, 1 Nov 2020 14:15:39 -0700 Message-Id: <20201101211544.3579850-6-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present this function always allocates a large amount of stack, and selects its own size for buffers. This makes it hard to test the code for buffer overflow. Separate out the inner logic of the substitution so that tests can call this directly. This will allow checking that the algorithm does not overflow the buffer. Fix up one of the error lines at the same time, since it should be printing nbuf_size, not data_size. Signed-off-by: Simon Glass --- cmd/setexpr.c | 156 +++++++++++++++++++++++++++++++------------------- 1 file changed, 98 insertions(+), 58 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index dd9c2574fdc..fe3435b4d99 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -58,9 +58,6 @@ static ulong get_arg(char *s, int w) #include -#define SLRE_BUFSZ 16384 -#define SLRE_PATSZ 4096 - /* * memstr - Find the first substring in memory * @s1: The string to be searched @@ -83,13 +80,24 @@ static char *memstr(const char *s1, int l1, const char *s2, int l2) return NULL; } -static char *substitute(char *string, /* string buffer */ - int *slen, /* current string length */ - int ssize, /* string bufer size */ - const char *old,/* old (replaced) string */ - int olen, /* length of old string */ - const char *new,/* new (replacement) string */ - int nlen) /* length of new string */ +/** + * substitute() - Substitute part of one string with another + * + * This updates @string so that the first occurrence of @old is replaced with + * @new + * + * @string: String buffer containing string to update at the start + * @slen: Pointer to current string length, updated on success + * @ssize: Size of string buffer + * @old: Old string to find in the buffer (no terminator needed) + * @olen: Length of @old excluding terminator + * @new: New string to replace @old with + * @nlen: Length of @new excluding terminator + * @return pointer to immediately after the copied @new in @string, or NULL if + * no replacement took place + */ +static char *substitute(char *string, int *slen, int ssize, + const char *old, int olen, const char *new, int nlen) { char *p = memstr(string, *slen, old, olen); @@ -118,7 +126,7 @@ static char *substitute(char *string, /* string buffer */ memmove(p + nlen, p + olen, tail); } - /* insert substitue */ + /* insert substitute */ memcpy(p, new, nlen); *slen += nlen - olen; @@ -126,52 +134,33 @@ static char *substitute(char *string, /* string buffer */ return p + nlen; } -/* - * Perform regex operations on a environment variable +/** + * regex_sub() - Replace a regex pattern with a string * - * Returns 0 if OK, 1 in case of errors. + * @data: Buffer containing the string to update + * @data_size: Size of buffer (must be large enough for the new string) + * @nbuf: Back-reference buffer + * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus + * all back-reference expansions) + * @r: Regular expression to find + * @s: String to replace with + * @global: true to replace all matches in @data, false to replace just the + * first + * @return 0 if OK, 1 on error */ -static int regex_sub(const char *name, - const char *r, const char *s, const char *t, - int global) +static int regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, + const char *r, const char *s, bool global) { struct slre slre; - char data[SLRE_BUFSZ]; char *datap = data; - const char *value; int res, len, nlen, loop; - if (name == NULL) - return 1; - if (slre_compile(&slre, r) == 0) { printf("Error compiling regex: %s\n", slre.err_str); return 1; } - if (t == NULL) { - value = env_get(name); - - if (value == NULL) { - printf("## Error: variable \"%s\" not defined\n", name); - return 1; - } - t = value; - } - - debug("REGEX on %s=%s\n", name, t); - debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", - r, s ? s : "", global); - - len = strlen(t); - if (len + 1 > SLRE_BUFSZ) { - printf("## error: subst buffer overflow: have %d, need %d\n", - SLRE_BUFSZ, len + 1); - return 1; - } - - strcpy(data, t); - + len = strlen(data); if (s == NULL) nlen = 0; else @@ -179,7 +168,6 @@ static int regex_sub(const char *name, for (loop = 0;; loop++) { struct cap caps[slre.num_caps + 2]; - char nbuf[SLRE_PATSZ]; const char *old; char *np; int i, olen; @@ -199,7 +187,7 @@ static int regex_sub(const char *name, if (res == 0) { if (loop == 0) { - printf("%s: No match\n", t); + printf("%s: No match\n", data); return 1; } else { break; @@ -208,17 +196,15 @@ static int regex_sub(const char *name, debug("## MATCH ## %s\n", data); - if (s == NULL) { - printf("%s=%s\n", name, t); + if (!s) return 1; - } old = caps[0].ptr; olen = caps[0].len; - if (nlen + 1 >= SLRE_PATSZ) { + if (nlen + 1 >= nbuf_size) { printf("## error: pattern buffer overflow: have %d, need %d\n", - SLRE_BUFSZ, nlen + 1); + nbuf_size, nlen + 1); return 1; } strcpy(nbuf, s); @@ -263,7 +249,7 @@ static int regex_sub(const char *name, break; np = substitute(np, &nlen, - SLRE_PATSZ, + nbuf_size, backref, 2, caps[i].ptr, caps[i].len); @@ -273,9 +259,8 @@ static int regex_sub(const char *name, } debug("## SUBST(2) ## %s\n", nbuf); - datap = substitute(datap, &len, SLRE_BUFSZ, - old, olen, - nbuf, nlen); + datap = substitute(datap, &len, data_size, old, olen, + nbuf, nlen); if (datap == NULL) return 1; @@ -289,6 +274,61 @@ static int regex_sub(const char *name, } debug("## FINAL (now env_set()) : %s\n", data); + return 0; +} + +#define SLRE_BUFSZ 16384 +#define SLRE_PATSZ 4096 + +/* + * Perform regex operations on a environment variable + * + * Returns 0 if OK, 1 in case of errors. + */ +static int regex_sub_var(const char *name, const char *r, const char *s, + const char *t, int global) +{ + struct slre slre; + char data[SLRE_BUFSZ]; + char nbuf[SLRE_PATSZ]; + const char *value; + int len; + int ret; + + if (!name) + return 1; + + if (slre_compile(&slre, r) == 0) { + printf("Error compiling regex: %s\n", slre.err_str); + return 1; + } + + if (!t) { + value = env_get(name); + if (!value) { + printf("## Error: variable \"%s\" not defined\n", name); + return 1; + } + t = value; + } + + debug("REGEX on %s=%s\n", name, t); + debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "", + global); + + len = strlen(t); + if (len + 1 > SLRE_BUFSZ) { + printf("## error: subst buffer overflow: have %d, need %d\n", + SLRE_BUFSZ, len + 1); + return 1; + } + + strcpy(data, t); + + ret = regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, global); + if (ret) + return 1; + printf("%s=%s\n", name, data); return env_set(name, data); @@ -331,10 +371,10 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, * with 5 args, "t" will be NULL */ if (strcmp(argv[2], "gsub") == 0) - return regex_sub(argv[1], argv[3], argv[4], argv[5], 1); + return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1); if (strcmp(argv[2], "sub") == 0) - return regex_sub(argv[1], argv[3], argv[4], argv[5], 0); + return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0); #endif /* standard operators: "setexpr name val1 op val2" */ From patchwork Sun Nov 1 21:15:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391867 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=hboxhJgx; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTQl1pH1z9sRR for ; Mon, 2 Nov 2020 08:17:35 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 11F4E8256D; Sun, 1 Nov 2020 22:16:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="hboxhJgx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7349882537; Sun, 1 Nov 2020 22:16:07 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd41.google.com (mail-io1-xd41.google.com [IPv6:2607:f8b0:4864:20::d41]) (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 01C498251C for ; Sun, 1 Nov 2020 22:15:59 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd41.google.com with SMTP id h21so13010284iob.10 for ; Sun, 01 Nov 2020 13:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=X1ZS/Z6XoUmz0fIaB30cl9ro+0rKOB/rm5XaOqf8np4=; b=hboxhJgxxyIPVoedvrRLcOzrznqppZesDL5rMb01rWZBiKmzeqv0ZQ/STxmcrXN6ts ihIjopquUYfSgnqgmDynZJMha2Utp6PamT54gxxMRjRW8ouozQmtDXsbXMtpljBt6+WX XTkSGVp5VG7Xy1+lcrycgNLU1ZHrRxz3n9Hc0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=X1ZS/Z6XoUmz0fIaB30cl9ro+0rKOB/rm5XaOqf8np4=; b=uhVOsNcqAu1OGEvYBkJGMb7r+DRCQW3C4f8Jjgfzwc60YcOpoq2ap0GwmPg0KVs+2C U5xoutxmtCYfdDAabN9yuToO7OvFugotLj2v4wEgr/dzPf5g5MWvYEAwmHRfnCClrXEu B8jstxp+UtNjFC+CwUfdjAzupBFzA7hmred1ryxl/QKcPQ5DITnRlEXVsJUj6jGLm1+c YPKstQN8JppjyDRYqtGe5etegUq5NfD2AW91kIYTEvN8VWglgNP3rBCMnlKWEfseeg4k NMJ5ryag0nxFGwrP2EMVvmxmkBBgxMXlPRt3WA5vhx2KCAAyU3PNiLp5Kp4r0Zaprzpa fs6g== X-Gm-Message-State: AOAM530Y+ublrvDO6uNEaPo3hcJd6J1+UQPM70cVi3Ay1eC5WtMSQk4K qnQNnJw4v5XgpYecyjDQ7IShLGCLwItRRyMB X-Google-Smtp-Source: ABdhPJzUyhMl1nS0VOh1K+2oOwxcfqtD8LyyB7ENGlQMwMSmvNcFdL7RJICDFgGE4AjO1Jpc8wMeXw== X-Received: by 2002:a05:6638:dcc:: with SMTP id m12mr9118477jaj.30.1604265358542; Sun, 01 Nov 2020 13:15:58 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:58 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 06/10] setexpr: Add some tests for buffer overflow and backref Date: Sun, 1 Nov 2020 14:15:40 -0700 Message-Id: <20201101211544.3579850-7-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean Add tests to check for buffer overflow using simple replacement as well as back references. At present these don't fully pass. Signed-off-by: Simon Glass --- cmd/setexpr.c | 21 +++-------- include/command.h | 17 +++++++++ test/cmd/setexpr.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 17 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index fe3435b4d99..dbb43b3be2f 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -134,22 +134,8 @@ static char *substitute(char *string, int *slen, int ssize, return p + nlen; } -/** - * regex_sub() - Replace a regex pattern with a string - * - * @data: Buffer containing the string to update - * @data_size: Size of buffer (must be large enough for the new string) - * @nbuf: Back-reference buffer - * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus - * all back-reference expansions) - * @r: Regular expression to find - * @s: String to replace with - * @global: true to replace all matches in @data, false to replace just the - * first - * @return 0 if OK, 1 on error - */ -static int regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, - const char *r, const char *s, bool global) +int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, + const char *r, const char *s, bool global) { struct slre slre; char *datap = data; @@ -325,7 +311,8 @@ static int regex_sub_var(const char *name, const char *r, const char *s, strcpy(data, t); - ret = regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, global); + ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, + global); if (ret) return 1; diff --git a/include/command.h b/include/command.h index e900f97df33..e229bf2825c 100644 --- a/include/command.h +++ b/include/command.h @@ -183,6 +183,23 @@ extern int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); #endif +/** + * setexpr_regex_sub() - Replace a regex pattern with a string + * + * @data: Buffer containing the string to update + * @data_size: Size of buffer (must be large enough for the new string) + * @nbuf: Back-reference buffer + * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus + * all back-reference expansions) + * @r: Regular expression to find + * @s: String to replace with + * @global: true to replace all matches in @data, false to replace just the + * first + * @return 0 if OK, 1 on error + */ +int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, + const char *r, const char *s, bool global); + /* * Error codes that commands return to cmd_process(). We use the standard 0 * and 1 for success and failure, but add one more case - failure with a diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index de54561917c..a6940fd82dd 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -209,6 +209,95 @@ static int setexpr_test_regex_inc(struct unit_test_state *uts) } SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC); +/* Test setexpr_regex_sub() directly to check buffer usage */ +static int setexpr_test_sub(struct unit_test_state *uts) +{ + char *buf, *nbuf; + int i; + + buf = map_sysmem(0, BUF_SIZE); + nbuf = map_sysmem(0x1000, BUF_SIZE); + + /* Add a pattern so we can check the buffer limits */ + memset(buf, '\xff', BUF_SIZE); + memset(nbuf, '\xff', BUF_SIZE); + for (i = BUF_SIZE; i < 0x1000; i++) { + buf[i] = i & 0xff; + nbuf[i] = i & 0xff; + } + strcpy(buf, "this is a test"); + + /* + * This is a regression test, since a bug was found in the use of + * memmove() in setexpr + */ + ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is", + "us it is longer", true)); + ut_asserteq_str("thus it is longer us it is longer a test", buf); + + /* The following checks fail at present due to a bug in setexpr */ + return 0; + for (i = BUF_SIZE; i < 0x1000; i++) { + ut_assertf(buf[i] == (char)i, + "buf byte at %x should be %02x, got %02x)\n", + i, i & 0xff, (u8)buf[i]); + ut_assertf(nbuf[i] == (char)i, + "nbuf byte at %x should be %02x, got %02x)\n", + i, i & 0xff, (u8)nbuf[i]); + } + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC); + +/* Test setexpr_regex_sub() with back references */ +static int setexpr_test_backref(struct unit_test_state *uts) +{ + char *buf, *nbuf; + int i; + + buf = map_sysmem(0, BUF_SIZE); + nbuf = map_sysmem(0x1000, BUF_SIZE); + + /* Add a pattern so we can check the buffer limits */ + memset(buf, '\xff', BUF_SIZE); + memset(nbuf, '\xff', BUF_SIZE); + for (i = BUF_SIZE; i < 0x1000; i++) { + buf[i] = i & 0xff; + nbuf[i] = i & 0xff; + } + strcpy(buf, "this is surely a test is it? yes this is indeed a test"); + + /* + * This is a regression test, since a bug was found in the use of + * memmove() in setexpr + */ + ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, + "(this) (is) (surely|indeed)", + "us \\1 \\2 \\3!", true)); + + /* The following checks fail at present due to bugs in setexpr */ + return 0; + ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test", + buf); + + for (i = BUF_SIZE; i < 0x1000; i++) { + ut_assertf(buf[i] == (char)i, + "buf byte at %x should be %02x, got %02x)\n", + i, i & 0xff, (u8)buf[i]); + ut_assertf(nbuf[i] == (char)i, + "nbuf byte at %x should be %02x, got %02x)\n", + i, i & 0xff, (u8)nbuf[i]); + } + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC); + int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = ll_entry_start(struct unit_test, From patchwork Sun Nov 1 21:15:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391869 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=VQzZbDwR; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTQx0hYKz9sRR for ; Mon, 2 Nov 2020 08:17:44 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 68AE78257B; Sun, 1 Nov 2020 22:16:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="VQzZbDwR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2C2E882553; Sun, 1 Nov 2020 22:16:10 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x143.google.com (mail-il1-x143.google.com [IPv6:2607:f8b0:4864:20::143]) (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 0615782541 for ; Sun, 1 Nov 2020 22:16:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x143.google.com with SMTP id a20so11202912ilk.13 for ; Sun, 01 Nov 2020 13:16:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ul0Bc/nRi6rCGa9K4aAqR0jSrbVQOcSR2DIV/Ys6miI=; b=VQzZbDwRPzjhEKvROwMmIzcC/P6Xq2oSs/48kp0ql0ZTbCvW6Hiox5F+YNiLI1bg7T Xokj67oCqr0dQkj5MFRVwgIp/i40ySQWzv1tDQ5xKpn/v1NGGd23OIZb6CvP23Y0zu1/ 2N1WCdjn+00E1KUW8TDFzZsV/mzIM3+S6muxo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ul0Bc/nRi6rCGa9K4aAqR0jSrbVQOcSR2DIV/Ys6miI=; b=IOn/A2TS4dXKn8sY16Peu1ZmOK2GpPa8djhRVxwmPmE7PdbsF/LK763LgyXJ6pMmoF WyG791SDmDINmgH2s8KT9/NfZSN7aMGz4cUq7VkVdMc9J975To6E+afV0cByj8EHGlkH zOEZU0WTB/wZQ28a3UUUohvm1pCiSQ+jqSQHkT9Qhi9yYvDQH0VVTsR75VS28OgG3XZV jSvnd5MH/rueE+ELETlwrCzK0JyIlRS6efgTEFhrfB+k06gcYW+r9g3DBnD46V4Lblev BCpY2GP6TWyz1ntOisPt8a+VNPkvE4YXf46X8O1D4Oian0mNFUjLZPvWPueTYopY300t tu7Q== X-Gm-Message-State: AOAM53187Jku+wiHbZgifa/l8wU0T/Zj8J2KUkax8X0rvY7g4vFOpDfA AlniC9yi6v/FgPPgYsmWLXclDCVAtrfFXq7o X-Google-Smtp-Source: ABdhPJxYaamgCZP+lL5PJOFkBBY9TFKVMrPiFj7jk7aRwdTghkxbsgFmKr2W8LRqrsntDccs4wzlDQ== X-Received: by 2002:a05:6e02:14c9:: with SMTP id o9mr8844205ilk.137.1604265359662; Sun, 01 Nov 2020 13:15:59 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:15:59 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 07/10] setexpr: Correct dropping of final unmatched string Date: Sun, 1 Nov 2020 14:15:41 -0700 Message-Id: <20201101211544.3579850-8-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present the 'nlen' variable increases with each loop. If the previous loop had back references, then subsequent loops without back references use the wrong value of nlen. The value is larger, meaning that the string terminator from nbuf is copied along to the main buffer, thus terminating the string prematurely. This leads to the final result being truncated, e.g. missing the last (unmatched) part of the string. So "match match tail" become "replaced replaced" instead of "replaced replaced tail". Fix this by resetting nlen to the correct value each time around the lop. Fixes: 855f18ea0e6 ("setexpr: add regex substring matching and substitution") Signed-off-by: Simon Glass --- cmd/setexpr.c | 6 +----- test/cmd/setexpr.c | 5 ++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index dbb43b3be2f..0cc7cf15bd7 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -147,11 +147,6 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, } len = strlen(data); - if (s == NULL) - nlen = 0; - else - nlen = strlen(s); - for (loop = 0;; loop++) { struct cap caps[slre.num_caps + 2]; const char *old; @@ -187,6 +182,7 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, old = caps[0].ptr; olen = caps[0].len; + nlen = strlen(s); if (nlen + 1 >= nbuf_size) { printf("## error: pattern buffer overflow: have %d, need %d\n", diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index a6940fd82dd..d06dda260e6 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -277,12 +277,11 @@ static int setexpr_test_backref(struct unit_test_state *uts) ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "(this) (is) (surely|indeed)", "us \\1 \\2 \\3!", true)); - - /* The following checks fail at present due to bugs in setexpr */ - return 0; ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test", buf); + /* The following checks fail at present due to a bug in setexpr */ + return 0; for (i = BUF_SIZE; i < 0x1000; i++) { ut_assertf(buf[i] == (char)i, "buf byte at %x should be %02x, got %02x)\n", From patchwork Sun Nov 1 21:15:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391871 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=MLnjK7DB; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTRH2yz2z9sRR for ; Mon, 2 Nov 2020 08:18:03 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 561CB8257E; Sun, 1 Nov 2020 22:16:41 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="MLnjK7DB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2757A82544; Sun, 1 Nov 2020 22:16:14 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd43.google.com (mail-io1-xd43.google.com [IPv6:2607:f8b0:4864:20::d43]) (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 46FD28254C for ; Sun, 1 Nov 2020 22:16:02 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd43.google.com with SMTP id z5so13057762iob.1 for ; Sun, 01 Nov 2020 13:16:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=q949dk8PUy7O4aPJnJV0gO0vnwt9TqQ5DQRgU66vT3U=; b=MLnjK7DBk8VJpudiKa1KtJjuLwVBruBWoLWNMfxChe8qDduN9sz2VT6CLN+bcLO64a ZGosedwwRLF2gNXJkcAwN709OG5E+aji1TDhy17dEinKsUZ2XrCV3gSg96DXs1YN1vOe w5XZ5xV167rfhHz6foh3RYNqgasEfjJuWbQE0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=q949dk8PUy7O4aPJnJV0gO0vnwt9TqQ5DQRgU66vT3U=; b=hT4sRgh+CFaxG7Fm6xG1vsEGF4hPn0kJ0wIuPr/43OpMqupUsVgFB4NnqjCBt60na6 CV6fLrzsg/b1F9vclN+q3j/NtcpBaCHvrq0kofmEplp9RdqpXghC6WCBNVO2Kb0MWqnZ /Fe15P/INwj4e/KE58POvOCvFFzHYR9Vu3A+vjf8PXIvsBwrRNwN8X1kIPEvY9vWjO6c LKCYlyiTa7gXM9HcasYxBuQbktzEGAEoWFDDocz64pRvSdm5fiRoOZT2EG7UI0U4GkPc hCBdHmt21dSjJmGtOI50Qr8j5RJymTSjOUimccYwYL9VhYECBf/pXxoMs+0Xa6q2LJfB wiMg== X-Gm-Message-State: AOAM531CXbKfMivvlCoD7mfvbGJAe3Ji3U0jS8ETOxTTzJLchUz4+dKZ dcQwuOE4NylUQ0pru6Y9xQdX2akitKY6T4L5 X-Google-Smtp-Source: ABdhPJyIyh+f1Phuv5uVAk5f6YPfl615z6EJz7v5lnfxhagsXqBI0ufA5+awrC1VclNLD5TaxlBnYw== X-Received: by 2002:a5d:9644:: with SMTP id d4mr8369557ios.199.1604265360633; Sun, 01 Nov 2020 13:16:00 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:16:00 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 08/10] setexpr: Correct buffer overflow bug and enable tests Date: Sun, 1 Nov 2020 14:15:42 -0700 Message-Id: <20201101211544.3579850-9-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present when more than one substitution is made this function overwrites its buffers. Fix this bug and update the tests now that they can pass. Also update the debug code to show all substrings, since at present it omits the final one. Fixes: 855f18ea0e6 ("setexpr: add regex substring matching and substitution") Signed-off-by: Simon Glass --- cmd/setexpr.c | 10 +++++----- test/cmd/setexpr.c | 24 +++++++----------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index 0cc7cf15bd7..d364dbc2bc5 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -155,11 +155,11 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, (void) memset(caps, 0, sizeof(caps)); - res = slre_match(&slre, datap, len, caps); + res = slre_match(&slre, datap, len - (datap - data), caps); debug("Result: %d\n", res); - for (i = 0; i < slre.num_caps; i++) { + for (i = 0; i <= slre.num_caps; i++) { if (caps[i].len > 0) { debug("Substring %d: [%.*s]\n", i, caps[i].len, caps[i].ptr); @@ -231,7 +231,7 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, break; np = substitute(np, &nlen, - nbuf_size, + nbuf_size - (np - nbuf), backref, 2, caps[i].ptr, caps[i].len); @@ -241,8 +241,8 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, } debug("## SUBST(2) ## %s\n", nbuf); - datap = substitute(datap, &len, data_size, old, olen, - nbuf, nlen); + datap = substitute(datap, &len, data_size - (datap - data), + old, olen, nbuf, nlen); if (datap == NULL) return 1; diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index d06dda260e6..2a897efd9bd 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -166,12 +166,10 @@ static int setexpr_test_regex(struct unit_test_state *uts) /* Global substitution */ ut_assertok(run_command("setenv fred 'this is a test'", 0)); - if (0) { - /* Causes a crash at present due to a bug in setexpr */ - ut_assertok(run_command("setexpr fred gsub is us", 0)); - val = env_get("fred"); - ut_asserteq_str("thus us a test", val); - } + ut_assertok(run_command("setexpr fred gsub is us", 0)); + val = env_get("fred"); + ut_asserteq_str("thus us a test", val); + /* Global substitution */ ut_assertok(run_command("setenv fred 'this is a test'", 0)); ut_assertok(run_command("setenv mary 'this is a test'", 0)); @@ -195,14 +193,9 @@ static int setexpr_test_regex_inc(struct unit_test_state *uts) buf = map_sysmem(0, BUF_SIZE); ut_assertok(run_command("setenv fred 'this is a test'", 0)); - if (0) { - /* Causes a crash at present due to a bug in setexpr */ - ut_assertok(run_command("setexpr fred gsub is much_longer_string", - 0)); - val = env_get("fred"); - ut_asserteq_str("thmuch_longer_string much_longer_string a test", - val); - } + ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0)); + val = env_get("fred"); + ut_asserteq_str("thmuch_longer_string much_longer_string a test", val); unmap_sysmem(buf); return 0; @@ -234,9 +227,6 @@ static int setexpr_test_sub(struct unit_test_state *uts) ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is", "us it is longer", true)); ut_asserteq_str("thus it is longer us it is longer a test", buf); - - /* The following checks fail at present due to a bug in setexpr */ - return 0; for (i = BUF_SIZE; i < 0x1000; i++) { ut_assertf(buf[i] == (char)i, "buf byte at %x should be %02x, got %02x)\n", From patchwork Sun Nov 1 21:15:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391870 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=QEGw7hz+; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTR634Vdz9sVK for ; Mon, 2 Nov 2020 08:17:54 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F292E82571; Sun, 1 Nov 2020 22:16:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="QEGw7hz+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 26A1082541; Sun, 1 Nov 2020 22:16:13 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x144.google.com (mail-il1-x144.google.com [IPv6:2607:f8b0:4864:20::144]) (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 EE46D8254F for ; Sun, 1 Nov 2020 22:16:02 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x144.google.com with SMTP id v18so11269305ilg.1 for ; Sun, 01 Nov 2020 13:16:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D4HxZR9OqPNwvOcNwEjOlYsLjPFJuizbcc1HbqgKxpo=; b=QEGw7hz+kVCr8xG6nYRNxy7IUszkOUhiDVWUY5N9aHJEF9O0oFMOzNseyYbtQZw6wS Iltx8tb4Q3wL+OOMsI1TFDX1y/JYk7+RWajeXM4t+ot2cd9kpQZ9VIPX+KxawpENdHB/ 1mNqWkMDEOdpNNwfO6pl4WxY2BfNPFGfUpMcA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=D4HxZR9OqPNwvOcNwEjOlYsLjPFJuizbcc1HbqgKxpo=; b=IN2/BSz28Ae9KBSaBlIxcAel+V5FywcQoJFaaj2EvMmU8X9oeE9eTBd4DyGyADS2xg zud2jXP/pr0InIXwSiGhtoTBABcRnv1OWA5DyrU9W2QXmDGRlHOeKsbEY8KISLK/CJcn isQ6sxLjbckQoiFEpnbRIkqOQ67cL7+VmVAMSMhbvr1O4zlmBi16CELLHfOsIivIQEhf w25dVGUS1bdfuX/zOY3OcGP8fFLe5ZIcivIPCDrQMYCO4vOaIzlHLA9TDZQAbnuSF4Aa YS+tw52tAec3zc/Ge7BLgU5r0Ks/+RmU5SVm4rFW8aTYPoVVKe8qLqH9A+M/HN3jIU2E /zGw== X-Gm-Message-State: AOAM533+kRB3Dt7wrszpi7RWwA/PEDi0xInERyPtRzHwOJY9DVzUcYCJ vv6XCtjKm3nUDmogTGiuu8WDcy6WKHxra3Tg X-Google-Smtp-Source: ABdhPJx/fBxNYvUCCEy7mSOAn1AN5o8ltNIWCuSVOJCYdwRdjeTSEL3fDCgwYclr0JCbcoXlaYgr3Q== X-Received: by 2002:a92:7914:: with SMTP id u20mr8530401ilc.203.1604265361623; Sun, 01 Nov 2020 13:16:01 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.16.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:16:01 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 09/10] setexpr: Convert to use a struct for values Date: Sun, 1 Nov 2020 14:15:43 -0700 Message-Id: <20201101211544.3579850-10-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean At present a ulong is used to hold operand values. This means that strings cannot be used. While most operations are not useful for strings, concatenation is. As a starting point to supporting strings, convert the code to use a struct instead of a ulong for operands. Signed-off-by: Simon Glass --- cmd/setexpr.c | 111 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 44 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index d364dbc2bc5..8a3654505da 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -15,8 +15,19 @@ #include #include -static ulong get_arg(char *s, int w) +/** + * struct expr_arg: Holds an argument to an expression + * + * @ival: Integer value (if width is not CMD_DATA_SIZE_STR) + */ +struct expr_arg { + ulong ival; +}; + +static int get_arg(char *s, int w, struct expr_arg *argp) { + struct expr_arg arg; + /* * If the parameter starts with a '*' then assume it is a pointer to * the value we want. @@ -32,26 +43,33 @@ static ulong get_arg(char *s, int w) p = map_sysmem(addr, sizeof(uchar)); val = (ulong)*(uchar *)p; unmap_sysmem(p); - return val; + arg.ival = val; + break; case 2: p = map_sysmem(addr, sizeof(ushort)); val = (ulong)*(ushort *)p; unmap_sysmem(p); - return val; + arg.ival = val; + break; case 4: p = map_sysmem(addr, sizeof(u32)); val = *(u32 *)p; unmap_sysmem(p); - return val; + arg.ival = val; + break; default: p = map_sysmem(addr, sizeof(ulong)); val = *p; unmap_sysmem(p); - return val; + arg.ival = val; + break; } } else { - return simple_strtoul(s, NULL, 16); + arg.ival = simple_strtoul(s, NULL, 16); } + *argp = arg; + + return 0; } #ifdef CONFIG_REGEX @@ -321,7 +339,7 @@ static int regex_sub_var(const char *name, const char *r, const char *s, static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - ulong a, b; + struct expr_arg aval, bval; ulong value; int w; @@ -339,13 +357,12 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, w = cmd_get_data_size(argv[0], 4); - a = get_arg(argv[2], w); + if (get_arg(argv[2], w, &aval)) + return CMD_RET_FAILURE; /* plain assignment: "setexpr name value" */ - if (argc == 3) { - env_set_hex(argv[1], a); - return 0; - } + if (argc == 3) + return env_set_hex(argv[1], aval.ival); /* 5 or 6 args (6 args only with [g]sub) */ #ifdef CONFIG_REGEX @@ -367,39 +384,45 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, if (strlen(argv[3]) != 1) return CMD_RET_USAGE; - b = get_arg(argv[4], w); - - switch (argv[3][0]) { - case '|': - value = a | b; - break; - case '&': - value = a & b; - break; - case '+': - value = a + b; - break; - case '^': - value = a ^ b; - break; - case '-': - value = a - b; - break; - case '*': - value = a * b; - break; - case '/': - value = a / b; - break; - case '%': - value = a % b; - break; - default: - printf("invalid op\n"); - return 1; - } + if (get_arg(argv[4], w, &bval)) + return CMD_RET_FAILURE; - env_set_hex(argv[1], value); + if (w != CMD_DATA_SIZE_STR) { + ulong a = aval.ival; + ulong b = bval.ival; + + switch (argv[3][0]) { + case '|': + value = a | b; + break; + case '&': + value = a & b; + break; + case '+': + value = a + b; + break; + case '^': + value = a ^ b; + break; + case '-': + value = a - b; + break; + case '*': + value = a * b; + break; + case '/': + value = a / b; + break; + case '%': + value = a % b; + break; + default: + printf("invalid op\n"); + return 1; + } + + env_set_hex(argv[1], value); + } return 0; } From patchwork Sun Nov 1 21:15:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1391872 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=a8f+ZA06; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CPTRS4zJjz9sRR for ; Mon, 2 Nov 2020 08:18:12 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A37348258A; Sun, 1 Nov 2020 22:16:47 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="a8f+ZA06"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2BEFC82560; Sun, 1 Nov 2020 22:16:19 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd43.google.com (mail-io1-xd43.google.com [IPv6:2607:f8b0:4864:20::d43]) (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 1325D82554 for ; Sun, 1 Nov 2020 22:16:04 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd43.google.com with SMTP id u21so2421520iol.12 for ; Sun, 01 Nov 2020 13:16:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gVKyalU/qxYAux3Poth7aKntqKRTjWSXQc+sZDd8nPg=; b=a8f+ZA06wFuWjBfqUBuXWR5W6PZtTA4Qg4OgIzZO/nHjJX6bxE9AJa5FN1c5yfZOUH E+TQRkDydig3bmz6/yqMxk9geN4am9o+5FtswR8frXkRO2qSNg6foTMNpAw6z2kvA9g+ +heoZxZYMu+64KW7A89dGplnjeVaD83T7hyrI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gVKyalU/qxYAux3Poth7aKntqKRTjWSXQc+sZDd8nPg=; b=uPts0DdzO6u0rpPAQVJ8aD35TKTtWzkQQeR3xXr3HARVXYndyOalYD+55ru3PwDvQa 5ZE/i23hne17deccC8BS8tuLCrSwMDIWEorYZDu7xOJH/Kk+ds3C31YhrewK0SwWEvjv eL7shqQTeZE3bFCcyEF3zJ4K6D1DnRktafhxCUvTLXTBt5Ofpw03zWt9OD0BcFKKMZfE 5xmDx4MK6ID46UBL27qRdFsAPSl8HkBV31xjHwNrppQQZ7Uix4ayPQAN/XuumawdXI/b BwpdQlWcw0SXhm76+mT0yEe6qUICs3FBiGpenFwlx3CnWwCqobFGrrs09/opdC1SfKF/ VLZw== X-Gm-Message-State: AOAM532rqvMqNJnK2KgwAWhYHSETOerU7RXrJsKcLEQSHSw3VLN5n+XK /pB3Fq4yj0MJ3VhM1qig2RTTtx+u3+kdKkGb X-Google-Smtp-Source: ABdhPJzim/30/FQ53d1zgxrZhX1pdG4pk8SOfB10jqS5ezvFne7SLX3X2J7NJ6Og6HvjtlS0UfKKHQ== X-Received: by 2002:a02:cd02:: with SMTP id g2mr9487476jaq.22.1604265362622; Sun, 01 Nov 2020 13:16:02 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm4131534iot.21.2020.11.01.13.16.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 13:16:02 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Wolfgang Denk , Simon Glass Subject: [PATCH 10/10] setexpr: Add support for strings Date: Sun, 1 Nov 2020 14:15:44 -0700 Message-Id: <20201101211544.3579850-11-sjg@chromium.org> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org> References: <20201101211544.3579850-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean Add support for dealing with string operands, including reading a string from memory into an environment variable and concatenating two strings. Signed-off-by: Simon Glass Acked-by: Marek BehĂșn --- cmd/setexpr.c | 82 +++++++++++++++++++++++++++++++++++++++---- test/cmd/setexpr.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 7 deletions(-) diff --git a/cmd/setexpr.c b/cmd/setexpr.c index 8a3654505da..e828be39700 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -13,15 +13,21 @@ #include #include #include +#include #include +#include /** * struct expr_arg: Holds an argument to an expression * * @ival: Integer value (if width is not CMD_DATA_SIZE_STR) + * @sval: String value (if width is CMD_DATA_SIZE_STR) */ struct expr_arg { - ulong ival; + union { + ulong ival; + char *sval; + }; }; static int get_arg(char *s, int w, struct expr_arg *argp) @@ -36,6 +42,8 @@ static int get_arg(char *s, int w, struct expr_arg *argp) ulong *p; ulong addr; ulong val; + int len; + char *str; addr = simple_strtoul(&s[1], NULL, 16); switch (w) { @@ -51,6 +59,21 @@ static int get_arg(char *s, int w, struct expr_arg *argp) unmap_sysmem(p); arg.ival = val; break; + case CMD_DATA_SIZE_STR: + p = map_sysmem(addr, SZ_64K); + + /* Maximum string length of 64KB plus terminator */ + len = strnlen((char *)p, SZ_64K) + 1; + str = malloc(len); + if (!str) { + printf("Out of memory\n"); + return -ENOMEM; + } + memcpy(str, p, len); + str[len - 1] = '\0'; + unmap_sysmem(p); + arg.sval = str; + break; case 4: p = map_sysmem(addr, sizeof(u32)); val = *(u32 *)p; @@ -65,6 +88,8 @@ static int get_arg(char *s, int w, struct expr_arg *argp) break; } } else { + if (w == CMD_DATA_SIZE_STR) + return -EINVAL; arg.ival = simple_strtoul(s, NULL, 16); } *argp = arg; @@ -341,6 +366,7 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, { struct expr_arg aval, bval; ulong value; + int ret = 0; int w; /* @@ -361,8 +387,16 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, return CMD_RET_FAILURE; /* plain assignment: "setexpr name value" */ - if (argc == 3) - return env_set_hex(argv[1], aval.ival); + if (argc == 3) { + if (w == CMD_DATA_SIZE_STR) { + ret = env_set(argv[1], aval.sval); + free(aval.sval); + } else { + ret = env_set_hex(argv[1], aval.ival); + } + + return ret; + } /* 5 or 6 args (6 args only with [g]sub) */ #ifdef CONFIG_REGEX @@ -384,10 +418,38 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, if (strlen(argv[3]) != 1) return CMD_RET_USAGE; - if (get_arg(argv[4], w, &bval)) + if (get_arg(argv[4], w, &bval)) { + if (w == CMD_DATA_SIZE_STR) + free(aval.sval); return CMD_RET_FAILURE; + } + + if (w == CMD_DATA_SIZE_STR) { + int len; + char *str; - if (w != CMD_DATA_SIZE_STR) { + switch (argv[3][0]) { + case '+': + len = strlen(aval.sval) + strlen(bval.sval) + 1; + str = malloc(len); + if (!str) { + printf("Out of memory\n"); + ret = CMD_RET_FAILURE; + } else { + /* These were copied out and checked earlier */ + strcpy(str, aval.sval); + strcat(str, bval.sval); + ret = env_set(argv[1], str); + if (ret) + printf("Could not set var\n"); + free(str); + } + break; + default: + printf("invalid op\n"); + ret = 1; + } + } else { ulong a = aval.ival; ulong b = bval.ival; @@ -424,15 +486,21 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, env_set_hex(argv[1], value); } - return 0; + if (w == CMD_DATA_SIZE_STR) { + free(aval.sval); + free(bval.sval); + } + + return ret; } U_BOOT_CMD( setexpr, 6, 0, do_setexpr, "set environment variable as the result of eval expression", - "[.b, .w, .l] name [*]value1 [*]value2\n" + "[.b, .w, .l, .s] name [*]value1 [*]value2\n" " - set environment variable 'name' to the result of the evaluated\n" " expression specified by . can be &, |, ^, +, -, *, /, %\n" + " (for strings only + is supported)\n" " size argument is only meaningful if value1 and/or value2 are\n" " memory addresses (*)\n" "setexpr[.b, .w, .l] name [*]value\n" diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c index 2a897efd9bd..fd6d869c0ed 100644 --- a/test/cmd/setexpr.c +++ b/test/cmd/setexpr.c @@ -287,6 +287,92 @@ static int setexpr_test_backref(struct unit_test_state *uts) } SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC); +/* Test 'setexpr' command with setting strings */ +static int setexpr_test_str(struct unit_test_state *uts) +{ + ulong start_mem; + char *buf; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\xff', BUF_SIZE); + + /* + * Set 'fred' to the same length as we expect to get below, to avoid a + * new allocation in 'setexpr'. That way we can check for memory leaks. + */ + ut_assertok(env_set("fred", "x")); + start_mem = ut_check_free(); + strcpy(buf, "hello"); + ut_asserteq(1, run_command("setexpr.s fred 0", 0)); + ut_assertok(ut_check_delta(start_mem)); + + start_mem = ut_check_free(); + ut_assertok(env_set("fred", "12345")); + ut_assertok(run_command("setexpr.s fred *0", 0)); + ut_asserteq_str("hello", env_get("fred")); + ut_assertok(ut_check_delta(start_mem)); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC); + + +/* Test 'setexpr' command with concatenating strings */ +static int setexpr_test_str_oper(struct unit_test_state *uts) +{ + ulong start_mem; + char *buf; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\xff', BUF_SIZE); + strcpy(buf, "hello"); + strcpy(buf + 0x10, " there"); + + ut_assertok(console_record_reset_enable()); + start_mem = ut_check_free(); + ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0)); + ut_assertok(ut_check_delta(start_mem)); + ut_assert_nextline("invalid op"); + ut_assert_console_end(); + + /* + * Set 'fred' to the same length as we expect to get below, to avoid a + * new allocation in 'setexpr'. That way we can check for memory leaks. + */ + ut_assertok(env_set("fred", "12345012345")); + start_mem = ut_check_free(); + ut_assertok(run_command("setexpr.s fred *0 + *10", 0)); + ut_asserteq_str("hello there", env_get("fred")); + ut_assertok(ut_check_delta(start_mem)); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC); + +/* Test 'setexpr' command with a string that is too long */ +static int setexpr_test_str_long(struct unit_test_state *uts) +{ + const int size = 128 << 10; /* setexpr strings are a max of 64KB */ + char *buf, *val; + + buf = map_sysmem(0, size); + memset(buf, 'a', size); + + /* String should be truncated to 64KB */ + ut_assertok(run_command("setexpr.s fred *0", 0)); + val = env_get("fred"); + ut_asserteq(64 << 10, strlen(val)); + + unmap_sysmem(buf); + + return 0; +} +SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC); + int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = ll_entry_start(struct unit_test,