From: Arav Verma <svav2021@gmail.com>
To: w@1wt.eu, linux@weissschuh.net
Cc: shuah@kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH 3/3] tools/nolibc: make atoi() and atol() skip whitespace and accept '+'
Date: Wed, 25 Nov 2026 12:14:53 -0600 [thread overview]
Message-ID: <20261125181453.814976-4-svav2021@gmail.com> (raw)
In-Reply-To: <20261125181453.814976-1-svav2021@gmail.com>
The C standard defines atoi() and atol() as strtol() with a fixed base
of 10, which skips leading whitespace and accepts an optional '+' or
'-' sign. nolibc only handles '-', so atoi(" 12") and atoi("+5") both
return 0.
Skip leading whitespace, accept '+' and add test cases.
Signed-off-by: Arav Verma <svav2021@gmail.com>
---
tools/include/nolibc/stdlib.h | 5 +++++
tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
2 files changed, 11 insertions(+)
diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 1c5a914c0f231..cccb8922bde7c 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -66,9 +66,14 @@ long atol(const char *s)
unsigned long d;
int neg = 0;
+ while (isspace(*s))
+ s++;
+
if (*s == '-') {
neg = 1;
s++;
+ } else if (*s == '+') {
+ s++;
}
while (1) {
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 971a7f48e016d..3062af07304a6 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -2097,6 +2097,12 @@ int run_stdlib(int min, int max)
CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, sizeof(long) == 8 ? (ptrdiff_t) 0x8000000000000000LL : (ptrdiff_t) 0x80000000); break;
CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, sizeof(long) == 8 ? (ptrdiff_t) 0x7fffffffffffffffLL : (ptrdiff_t) 0x7fffffff); break;
CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, sizeof(long) == 8 ? (size_t) 0xffffffffffffffffULL : (size_t) 0xffffffffU); break;
+ CASE_TEST(atoi_simple); EXPECT_EQ(1, atoi("35"), 35); break;
+ CASE_TEST(atoi_negative); EXPECT_EQ(1, atoi("-35"), -35); break;
+ CASE_TEST(atoi_positive); EXPECT_EQ(1, atoi("+35"), 35); break;
+ CASE_TEST(atoi_space); EXPECT_EQ(1, atoi(" \t\n35"), 35); break;
+ CASE_TEST(atoi_space_negative); EXPECT_EQ(1, atoi(" -35"), -35); break;
+ CASE_TEST(atol_space_positive); EXPECT_EQ(1, atol(" +35"), 35L); break;
CASE_TEST(strtol_simple); EXPECT_STRTOX(1, strtol, "35", 10, 35, -1, 0); break;
CASE_TEST(strtol_positive); EXPECT_STRTOX(1, strtol, "+35", 10, 35, -1, 0); break;
CASE_TEST(strtol_negative); EXPECT_STRTOX(1, strtol, "-35", 10, -35, -1, 0); break;
--
2.47.3
prev parent reply other threads:[~2026-11-25 18:14 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 18:14 [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions Arav Verma
2026-11-25 18:14 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
2026-11-25 18:14 ` [PATCH 2/3] tools/nolibc: fix strstr() with an empty needle Arav Verma
2026-11-25 18:14 ` Arav Verma [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125181453.814976-4-svav2021@gmail.com \
--to=svav2021@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=shuah@kernel.org \
--cc=w@1wt.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®