Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11B87C433F5 for ; Mon, 25 Apr 2022 05:33:16 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BA81283EAA; Mon, 25 Apr 2022 07:32:46 +0200 (CEST) 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="RYY2wmKI"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1748683E07; Mon, 25 Apr 2022 07:32:26 +0200 (CEST) Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) (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 C1ED683DF2 for ; Mon, 25 Apr 2022 07:32:09 +0200 (CEST) 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-ed1-x52f.google.com with SMTP id k27so1882376edk.4 for ; Sun, 24 Apr 2022 22:32:09 -0700 (PDT) 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=z6Ax3uIUyUWF+eg934W4ax2rG1um4HMMAY/Uw5eUVto=; b=RYY2wmKIaD8JhYCW01k0yP0HmesUfYL7Hu8ylZSwroBLPStQWL0pu2hF2hj6ijElIt G+eyZrhj3yvAghj6ZjifHXEAv6Yb2QvB5dAY7YhU7iUkHiCdj4v07oJam1F0qFEovHHV +CfzCLNK0nqFDZwBtrnaPEVXq5Hb4oQFSgiYQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z6Ax3uIUyUWF+eg934W4ax2rG1um4HMMAY/Uw5eUVto=; b=H9ACiH/rAm9Nr/j2HSQgRBZ4VXEUYAcDn/Jf7hLB1fc3mtjqhYN5bUrSR4kxlSHCkN 0fwHSJMP3+t/xDOxkktEJJdOfueIdCwoJzNPH1kEH2VEvB08LWHhkgKYnKwvXHtKBFtz MGcy3Nri6oXH5F0gQ71Quq30X6FIjPM2uZR2JVohj1VBqID95QoDluLwIL7Dpkmahotz CCTLpXmTiJnZBlsRHfR0A04a+CwndituAsYHd7kJAY3utdZxM8zy+bb4C/11aCPjl5oB imNuSPcvFGDYuDtqK3bkui7+RYDQgl0Q8pAouaqufbqlfy3L8LriTEM7akF1pQjrfVJ2 bUkg== X-Gm-Message-State: AOAM530U4nmKACJw6ilSoS51Z10xf3Q037TGHQymbA2DIwtfn5DB6F6S 10qp7V9YQOM5Tc1PuuPC1iK4yFQ/y3dRN7lk X-Google-Smtp-Source: ABdhPJyUUoGdFALtrTk9HFUMpI/d00/KpyCLVHripb5DZejSTVUoHiqGY+giijnLfPVpmqKyjLxdtA== X-Received: by 2002:a05:6402:2d6:b0:425:ed85:46bf with SMTP id b22-20020a05640202d600b00425ed8546bfmr1750920edx.6.1650864729170; Sun, 24 Apr 2022 22:32:09 -0700 (PDT) Received: from sjg1.c.hospitality.swisscom.com (d54c768e3.static.telenet.be. [84.199.104.227]) by smtp.gmail.com with ESMTPSA id s12-20020a1709062ecc00b006e8558c9a5csm3230784eji.94.2022.04.24.22.32.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Apr 2022 22:32:08 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Dennis Gilmore , Lukas Auer , Daniel Schwierzeck , Tom Rini , Michal Simek , Ilias Apalodimas , Steffen Jaeckel , Simon Glass Subject: [PATCH v5 04/34] lib: Fix a few bugs in trailing_strtoln() Date: Sun, 24 Apr 2022 23:30:57 -0600 Message-Id: <20220425053127.19950-5-sjg@chromium.org> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog In-Reply-To: <20220425053127.19950-1-sjg@chromium.org> References: <20220425053127.19950-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean At present this has a minor bug in that it reads the byte before the start of the string, if it is empty. Also it doesn't handle a non-numeric prefix which is only one character long. Fix these bugs with a reworked implementation. Add a test for the second case. The first one is hard to test. Signed-off-by: Simon Glass --- (no changes since v4) Changes in v4: - Correct the commit tag - Reword the commit message - Update the function comment to clarify its intert / limitations include/vsprintf.h | 3 +++ lib/strto.c | 11 ++++++----- test/str_ut.c | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/vsprintf.h b/include/vsprintf.h index d4bf3211da4..5172ceedec1 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -98,6 +98,9 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base); * Given a string this finds a trailing number on the string and returns it. * For example, "abc123" would return 123. * + * Note that this does not handle a string without a prefix. See dectoul() for + * that case. + * * @str: String to examine * Return: trailing number if found, else -1 */ diff --git a/lib/strto.c b/lib/strto.c index f1918843765..b1d803a77d9 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -189,11 +189,12 @@ long trailing_strtoln(const char *str, const char *end) if (!end) end = str + strlen(str); - if (isdigit(end[-1])) { - for (p = end - 1; p > str; p--) { - if (!isdigit(*p)) - return dectoul(p + 1, NULL); - } + p = end - 1; + if (p > str && isdigit(*p)) { + do { + if (!isdigit(p[-1])) + return dectoul(p, NULL); + } while (--p > str); } return -1; diff --git a/test/str_ut.c b/test/str_ut.c index 9674a59f2a6..058b3594379 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -257,6 +257,8 @@ static int str_trailing(struct unit_test_state *uts) ut_asserteq(123, trailing_strtoln(str1, str1 + 6)); ut_asserteq(-1, trailing_strtoln(str1, str1 + 9)); + ut_asserteq(3, trailing_strtol("a3")); + return 0; } STR_TEST(str_trailing, 0); -- 2.36.0.rc2.479.g8af0fa9b8e-goog