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",