From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 800F1C433F5 for ; Wed, 19 Jan 2022 01:44:17 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9A782837E9; Wed, 19 Jan 2022 02:43: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=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="Ea0gy88W"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EC6E7837CF; Wed, 19 Jan 2022 02:43:48 +0100 (CET) Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) (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 CC3F08303A for ; Wed, 19 Jan 2022 02:43:42 +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-xd29.google.com with SMTP id z19so991999ioj.1 for ; Tue, 18 Jan 2022 17:43:42 -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=DVK862rKaHR+OZMTXDfBWXtgOHjZyxARtD3NzJpld4U=; b=Ea0gy88WalEmxtE4j3pwt66g4hAMvKw9FFP9CBFFGwgxpzLXLHZ6DwDql8jSp/MFn3 I32L2tfWzHwXB3wXz6oDcOdLJSSvRzJH4FDd1M8xn02sN3fqSm1KXSZJd8lbHwywRf0y l9iwVIXXGReZJbQNlwSKefyFUhC/H4Vg91udk= 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=DVK862rKaHR+OZMTXDfBWXtgOHjZyxARtD3NzJpld4U=; b=57QMx1RGUExBfh0hOyygk8iOG/m+38vmGHz2ykhW7zsxUXP8asS2bGz5QpzWhZlJCC 1aKx2OVqavP3hLBGV2tnFepDPsk6XevcszXkfKdlUCsWGaURMONwuFtYLE+CIxx3XVpL cafvo3/cKlZ5te5Pxbgwe9RWjpkMHJn4hAY3VPgHxQtVk3ZgJb7w3RfUfypElGz0mvSX fbBbgKbd0n9wDnzwvAAPFOKoQKE5GcmDflgLjqbaVTzbISuzc/Qo9YJKJ0B3LF7wYSly b6xxklT97B67ZMgN2gwD/hq1xD9lHDNu0L9/0QdzazE4XHdtbIK2oKoFZdgu6AK+EMVj mfjg== X-Gm-Message-State: AOAM531T0t/x3RCc1G3zOyfIgAcLHr0fYX7S8O9WCMLytWPdSvWo/Y3i fjw+SNfb/h/8CQD7mBybIM+uuakQ/sqf1A== X-Google-Smtp-Source: ABdhPJy/3FQBWFUFx/IzoWwJcP38qfTHghWFFVs5j6JvzZrqwFs7vcFKYOEQiXDEXPcT3007eZJ8LQ== X-Received: by 2002:a5e:df4b:: with SMTP id g11mr6766362ioq.146.1642556621464; Tue, 18 Jan 2022 17:43:41 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id j18sm10546314ila.86.2022.01.18.17.43.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jan 2022 17:43:41 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Ilias Apalodimas , Daniel Schwierzeck , Dennis Gilmore , Steffen Jaeckel , Lukas Auer , Michal Simek , Tom Rini , Heinrich Schuchardt , Simon Glass Subject: [PATCH v3 03/31] str: Fix a few bugs in trailing_strtoln() Date: Tue, 18 Jan 2022 18:42:47 -0700 Message-Id: <20220119014315.1938157-4-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.703.g22d0c6ccf7-goog In-Reply-To: <20220119014315.1938157-1-sjg@chromium.org> References: <20220119014315.1938157-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.2 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. 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 v1) lib/strto.c | 11 ++++++----- test/str_ut.c | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/strto.c b/lib/strto.c index 7bba1e3e549..58fc10ecaea 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.34.1.703.g22d0c6ccf7-goog