Skip to contentSkip to conversation

tools/nolibc: fix standard conformance of a few string functions

PATCH New Arav Verma 1 participant 4 messages Nov 25

Subsystems (2)
lore
Share & tools
lore

Arav Verma

author
#0 lore raw

cover tools/nolibc: fix standard conformance of a few string functions

A few nolibc functions deviate from the C standard in corner cases:
strchr()/strrchr() do not find the terminating NUL, strstr() returns NULL for an empty needle, and atoi()/atol() do not skip leading whitespace or accept a '+' sign.

Fix them and add test cases. The new tests fail without the fixes. The results were also compared against glibc.

Arav Verma (3):
tools/nolibc: fix strchr() and strrchr() for the NUL terminator tools/nolibc: fix strstr() with an empty needle tools/nolibc: make atoi() and atol() skip whitespace and accept '+'

 tools/include/nolibc/stdlib.h                |  5 +++++
 tools/include/nolibc/string.h                | 12 +++++-------
 tools/testing/selftests/nolibc/nolibc-test.c |  9 +++++++++
 3 files changed, 19 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 1/3 tools/nolibc: fix strchr() and strrchr() for the NUL terminator

The C standard says that the terminating NUL character is considered part of the string, so strchr(s, '\0') and strrchr(s, '\0') must return a pointer to the terminator. Both functions stop before it and return NULL instead.

Check the terminator too and add test cases.

---
 tools/include/nolibc/string.h                | 10 ++++------
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 6 insertions(+), 6 deletions(-)
tools/include/nolibc/string.h +4 -6
index 4000926f44ac4..2693b9bef43a6 100644
@@ -111,11 +111,10 @@ void *memchr(const void *s, int c, size_t len)
 static __attribute__((unused))
 char *strchr(const char *s, int c)
 {
-	while (*s) {
+	do {
 		if (*s == (char)c)
 			return (char *)s;
-		s++;
-	}
+	} while (*s++);
 	return NULL;
 }
 
@@ -299,11 +298,10 @@ char *strrchr(const char *s, int c)
 {
 	const char *ret = NULL;
 
-	while (*s) {
+	do {
 		if (*s == (char)c)
 			ret = s;
-		s++;
-	}
+	} while (*s++);
 	return (char *)ret;
 }
 
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
index 1157b521e65b1..9e1d0f3afb901 100644
@@ -2028,8 +2028,10 @@ int run_stdlib(int min, int max)
 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
+		CASE_TEST(strchr_foobar_nul);  EXPECT_STREQ(1, strchr("foobar", '\0'), ""); break;
 		CASE_TEST(strrchr_foobar_o);   EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
 		CASE_TEST(strrchr_foobar_z);   EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
+		CASE_TEST(strrchr_foobar_nul); EXPECT_STREQ(1, strrchr("foobar", '\0'), ""); break;
 		CASE_TEST(strlcat_0);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 0), buf, 3, "test"); break;
 		CASE_TEST(strlcat_1);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 1), buf, 4, "test"); break;
 		CASE_TEST(strlcat_5);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 5), buf, 7, "test"); break;
Signature · 1 line
2.47.3
author
#2 lore raw

PATCH 2/3 tools/nolibc: fix strstr() with an empty needle

In reply to Arav Verma

The C standard says that strstr() returns the haystack if the needle is an empty string. nolibc returns NULL instead.

Return the haystack in that case and add a test case.

---
 tools/include/nolibc/string.h                | 2 +-
 tools/testing/selftests/nolibc/nolibc-test.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/include/nolibc/string.h +1 -1
index 2693b9bef43a6..191a3bc8af29f 100644
@@ -312,7 +312,7 @@ char *strstr(const char *haystack, const char *needle)
 
 	len_needle = strlen(needle);
 	if (!len_needle)
-		return NULL;
+		return (char *)haystack;
 
 	len_haystack = strlen(haystack);
 	while (len_haystack >= len_needle) {
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
index 9e1d0f3afb901..971a7f48e016d 100644
@@ -2046,6 +2046,7 @@ int run_stdlib(int min, int max)
 		CASE_TEST(strstr_foobar_foo);  EXPECT_STREQ(1, strstr("foobar", "foo"), "foobar"); break;
 		CASE_TEST(strstr_foobar_bar);  EXPECT_STREQ(1, strstr("foobar", "bar"), "bar"); break;
 		CASE_TEST(strstr_foobar_baz);  EXPECT_PTREQ(1, strstr("foobar", "baz"), NULL); break;
+		CASE_TEST(strstr_foobar_empty); EXPECT_STREQ(1, strstr("foo", ""), "foo"); break;
 		CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
 		CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
 		CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
Signature · 1 line
2.47.3
author
#3 lore raw

PATCH 3/3 tools/nolibc: make atoi() and atol() skip whitespace and accept '+'

In reply to Arav Verma

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.

---
 tools/include/nolibc/stdlib.h                | 5 +++++
 tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
 2 files changed, 11 insertions(+)
tools/include/nolibc/stdlib.h +5 -0
index 1c5a914c0f231..cccb8922bde7c 100644
@@ -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) {
tools/testing/selftests/nolibc/nolibc-test.c +6 -0
index 971a7f48e016d..3062af07304a6 100644
@@ -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;
Signature · 1 line
2.47.3