Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1240244lqd; Thu, 25 Apr 2024 09:30:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVjoqYdYxVS5mtCXOsvFvkZb7B3jz5nGxi3MVLuiYN6wkz3YzIEle8Vn1DqZ9j/Sxmis1Qz/D2jk7stgJ9rCAyLF//AQs9J0JGow2Kp7g== X-Google-Smtp-Source: AGHT+IGD8D5E5CxAj+za1cTSpqDOK5VAfJ7GoQXVwkgCa+wTftzRWZqOPra7kcsMGbbWkXo8nh6n X-Received: by 2002:a17:902:a518:b0:1e4:51ab:fffb with SMTP id s24-20020a170902a51800b001e451abfffbmr27467plq.25.1714062643479; Thu, 25 Apr 2024 09:30:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714062643; cv=pass; d=google.com; s=arc-20160816; b=v6NwivU163pDrFZbPtKiBqH14YEcumqwrHtnjivXaGEI2rpyGPdUGoOEgn4k4jCAf/ Ha8VMwLGAhPIvdALn8z+N3oWrptq0X4/z6fU0wENcPHGKTAD5M02DCnrQwkjT80vMgac 64elGmM9qQ4QFZH9NEaLK2YlQLw25qpYImj6ZU+vNqF9oJq6eC6Q71cZx13L1FSJ6vOy h1V3rhPiEBYnKn059IC0DlLBdROW/NT0BHKLaRDCAksiWwpd4RwPvZmWcWbbWv2C25hC XW2kb2tToQr5VCg9fGeByGxp0kgCt59woGXAuTxkv9T2DRDfjpjqxCxw4zpLqQb+sMP/ En1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=KliGCMJMlj0FPpCvZ69vqE6oUSa0bAhJPovGmBYQ3gc=; fh=Qbk0RS4bf6GjbhGkpgFoVgYg5V+1DfWcZ6CAErfO2MQ=; b=DqK950pMHQ8PKzp6s8/R5f26XaRwHzuiEqJ2FUnk7rORZbojGMoEeM6K2p6fwln+hx oz63b7BnkHFSIWQKFc65cbIYI8vpA8UPZ0jNJP7kWee863tTWXZ1pLJA4MkyvGdDYj2d fYM7gi57eJIHwggODqg31P2lslUEyZbzWfpiX2HU1jXA2fCmaqZR3FMIHTT93GDVTMuQ C/w5sA6cl66g1HcnLX4t8TBPJSDUCeiM5oMiXRpF5s5Lj8nXzOEZ3s4FmVELkbz+u8q/ uxvq2jitPkFDFqeWLLM7c6qSsRuijA335btGcX+sS2mQbNF3I8f4hSA/6Oid9ZVLlvQM DUjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=QG2lSSwQ; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-158870-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158870-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t13-20020a170902e84d00b001e55548a763si13346827plg.477.2024.04.25.09.30.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 09:30:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158870-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=QG2lSSwQ; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-158870-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158870-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 756FAB278BA for ; Thu, 25 Apr 2024 16:10:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A488714D2B6; Thu, 25 Apr 2024 16:10:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="QG2lSSwQ" Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8982114C59B; Thu, 25 Apr 2024 16:09:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714061399; cv=none; b=AqMVZMgzU05nR5lI5ygM2f7IIYjzz6ZFTIOzlbLpnWL2fdvo/K/QHRsTZ+ajh3knHFenFFPTb/86j2ebqPBWUAcPMDKyRLidMVM+zI8KsELgMX7LlkCqiEt8CxtLhHgGcBJprAtLhnP9BfUKrWQPlfvBkkDb+cxVJwID+PBbSC8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714061399; c=relaxed/simple; bh=3/vMU+KnL383rOtQme6UbwXiHGy/UeFxMMnLRj4gCZE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GC0ZmhfOi32wPkuyIrBnIUt9720WxOTDzDIBTc4eOB9VvrxbTvZEDtUW4uL21EWQLEFtdMbPeB11R1QmxZ4YtxZK4jIbooS3pkxi+vTcf8idrIOSc9NU1j3RTNAIHmOeQVztKcn5+6IuXGTtZq4cxD64aiHZ6DV+b02KqE/FNp8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=QG2lSSwQ; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1714061393; bh=3/vMU+KnL383rOtQme6UbwXiHGy/UeFxMMnLRj4gCZE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QG2lSSwQne7PHdSMNIIVxDuyZgk9baoGv4y9YbmxXMxaYMPvE+fHoijRo4R8Dfhxd JSmNftOOQk8Bxx47d+H/0i6jYSEUtAVMLjdLmNvNpTIi+W6eFHp/+g0qDGtNUuHfKt 9mBFrUYQbY8U62deY7cLOJojM9J8ZSF1iuCvfgeo= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 25 Apr 2024 18:09:27 +0200 Subject: [PATCH 2/2] tools/nolibc: implement strtol() and friends Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20240425-nolibc-strtol-v1-2-bfeef7846902@weissschuh.net> References: <20240425-nolibc-strtol-v1-0-bfeef7846902@weissschuh.net> In-Reply-To: <20240425-nolibc-strtol-v1-0-bfeef7846902@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1714061393; l=6945; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=3/vMU+KnL383rOtQme6UbwXiHGy/UeFxMMnLRj4gCZE=; b=ALKzAWq1VPUd2oO06ghnPQWfc6psbr8i0vEazGC7mOizZd07raUSAQzgpghSdvbxoR7gmfwsY GsxifsuwCI1DAJeloXw31irF+df7B/ZqMW++DbY0a/A1+Qg0gHfsUeJ X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The implementation always works on uintmax_t values. This is inefficient when only 32bit are needed. However for all functions this only happens for strtol() on 32bit platforms. Signed-off-by: Thomas Weißschuh --- tools/include/nolibc/stdlib.h | 109 +++++++++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 59 +++++++++++++++ 2 files changed, 168 insertions(+) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 5be9d3c7435a..f66870e25389 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -438,6 +438,115 @@ char *u64toa(uint64_t in) return itoa_buffer; } +static __attribute__((unused)) +uintmax_t __strtox(const char *nptr, char **endptr, int base, intmax_t lower_limit, uintmax_t upper_limit) +{ + const char signed_ = lower_limit != 0; + unsigned char neg = 0, overflow = 0; + uintmax_t val = 0, limit, old_val; + char c; + + if (base < 0 || base > 35) { + SET_ERRNO(EINVAL); + goto out; + } + + while (isspace(*nptr)) + nptr++; + + if (*nptr == '+') { + nptr++; + } else if (*nptr == '-') { + neg = 1; + nptr++; + } + + if (signed_ && neg) + limit = -(uintmax_t)lower_limit; + else + limit = upper_limit; + + if ((base == 0 || base == 16) && + (strncmp(nptr, "0x", 2) == 0 || strncmp(nptr, "0X", 2) == 0)) { + base = 16; + nptr += 2; + } else if (base == 0 && strncmp(nptr, "0", 1) == 0) { + base = 8; + nptr += 1; + } else if (base == 0) { + base = 10; + } + + while (*nptr) { + c = *nptr; + + if (c >= '0' && c <= '9') + c -= '0'; + else if (c >= 'a' && c <= 'z') + c = c - 'a' + 10; + else if (c >= 'A' && c <= 'Z') + c = c - 'A' + 10; + else + goto out; + + if (c > base) + goto out; + + nptr++; + old_val = val; + val *= base; + val += c; + + if (val > limit || val < old_val) + overflow = 1; + } + +out: + if (overflow) { + SET_ERRNO(ERANGE); + val = limit; + } + if (endptr) + *endptr = (char *)nptr; + return (neg ? -1 : 1) * val; +} + +static __attribute__((unused)) +long strtol(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, LONG_MIN, LONG_MAX); +} + +static __attribute__((unused)) +unsigned long strtoul(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, 0, ULONG_MAX); +} + +static __attribute__((unused)) +long long strtoll(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, LLONG_MIN, LLONG_MAX); +} + +static __attribute__((unused)) +unsigned long long strtoull(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, 0, ULLONG_MAX); +} + +static __attribute__((unused)) +intmax_t strtoimax(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, INTMAX_MIN, INTMAX_MAX); +} + +static __attribute__((unused)) +uintmax_t strtoumax(const char *nptr, char **endptr, int base) +{ + return __strtox(nptr, endptr, base, 0, UINTMAX_MAX); +} + /* make sure to include all global symbols */ #include "nolibc.h" diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index b9c84d42edbe..6161bd57a0c9 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -621,6 +621,51 @@ int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, const return 0; } +#define EXPECT_STRTOX(cond, func, input, base, expected, chars, expected_errno) \ + do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strtox(llen, func, input, base, expected, chars, expected_errno); } while (0) + +static __attribute__((unused)) +int expect_strtox(int llen, void *func, const char *input, int base, intmax_t expected, int expected_chars, int expected_errno) +{ + char *endptr; + int actual_errno, actual_chars; + intmax_t r; + + errno = 0; + if (func == strtol) { + r = strtol(input, &endptr, base); + } else if (func == strtoul) { + r = strtoul(input, &endptr, base); + } else { + result(llen, FAIL); + return 1; + } + actual_errno = errno; + actual_chars = endptr - input; + + llen += printf(" %lld = %lld", (long long)expected, (long long)r); + if (r != expected) { + result(llen, FAIL); + return 1; + } + if (expected_chars == -1) { + if (*endptr != '\0') { + result(llen, FAIL); + return 1; + } + } else if (expected_chars != actual_chars) { + result(llen, FAIL); + return 1; + } + if (actual_errno != expected_errno) { + result(llen, FAIL); + return 1; + } + + result(llen, OK); + return 0; +} + /* declare tests based on line numbers. There must be exactly one test per line. */ #define CASE_TEST(name) \ case __LINE__: llen += printf("%d %s", test, #name); @@ -1143,6 +1188,20 @@ 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(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; + CASE_TEST(strtol_hex_auto); EXPECT_STRTOX(1, strtol, "0xFF", 0, 255, -1, 0); break; + CASE_TEST(strtol_octal_auto); EXPECT_STRTOX(1, strtol, "011", 0, 9, -1, 0); break; + CASE_TEST(strtol_hex_00); EXPECT_STRTOX(1, strtol, "0x00", 16, 0, -1, 0); break; + CASE_TEST(strtol_hex_FF); EXPECT_STRTOX(1, strtol, "FF", 16, 255, -1, 0); break; + CASE_TEST(strtol_hex_ff); EXPECT_STRTOX(1, strtol, "ff", 16, 255, -1, 0); break; + CASE_TEST(strtol_hex_prefix); EXPECT_STRTOX(1, strtol, "0xFF", 16, 255, -1, 0); break; + CASE_TEST(strtol_trailer); EXPECT_STRTOX(1, strtol, "35foo", 10, 35, 2, 0); break; + CASE_TEST(strtol_overflow); EXPECT_STRTOX(1, strtol, "0x8000000000000000", 16, LONG_MAX, -1, ERANGE); break; + CASE_TEST(strtol_underflow); EXPECT_STRTOX(1, strtol, "-0x8000000000000001", 16, LONG_MIN, -1, ERANGE); break; + CASE_TEST(strtoul_negative); EXPECT_STRTOX(1, strtoul, "-0x1", 16, ULONG_MAX, 4, 0); break; + CASE_TEST(strtoul_overflow); EXPECT_STRTOX(1, strtoul, "0x10000000000000000", 16, ULONG_MAX, -1, ERANGE); break; case __LINE__: return ret; /* must be last */ -- 2.44.0