From nobody Mon Feb 9 16:50:39 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 47407246BC6; Wed, 7 Jan 2026 02:35:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767753351; cv=none; b=flM84KYJcl7L+bO11DeTYtQd/H/u4OGXEcTxYcfiQiACO5XB/EPcRZLgTEzLWT18EUjFunZWkAb7wFH4ArYRitwVGZswoxeHctcmdl16nR0txYeSSBgjMav13j3jtFpY1saDlWw2Gq5866OiBNpshHHWj3mDBgxDOzjPPLot+ZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767753351; c=relaxed/simple; bh=lXkmU50bTEVk27OLzbhU0W9yswrTlOSXVzzeQxSRumo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K1C8pcsuCuM4ItVWsxNNL4D+D/tpEuWpNQXBknBz1Jt3nWutXu6aV5OxGKs9Wx0m/D5fH5zRcx/gF1cO1OGZ+O+x82uwh0z5qHikxQLDegPE6cP3E5e3euPlyRBkpSt4VAztM8obWCYXT8Hn4rWdnkD84wihWEBxyTnYl1yUU5o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 8fd19cdaeb7111f0a38c85956e01ac42-20260107 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:0b59dcfc-61be-4163-8256-0e9613874fa2,IP:0,UR L:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:-5 X-CID-META: VersionHash:a9d874c,CLOUDID:793e51c660485b9dec2c3df733cba7b8,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 8fd19cdaeb7111f0a38c85956e01ac42-20260107 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 515336848; Wed, 07 Jan 2026 10:35:42 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, Jason@zx2c4.com, conor.dooley@microchip.com, samuel.holland@sifive.com, charlie@rivosinc.com, ajones@ventanamicro.com, nathan@kernel.org, linus.walleij@linaro.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/5] lib/string_kunit: add test case for strnlen Date: Wed, 7 Jan 2026 10:35:14 +0800 Message-Id: <20260107023517.46840-3-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260107023517.46840-1-jiangfeng@kylinos.cn> References: <20260107023517.46840-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments. Signed-off-by: Feng Jiang --- lib/tests/string_kunit.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 9f4fc72f0886..1d08850e439c 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -126,6 +126,35 @@ static void string_test_strlen(struct kunit *test) } } =20 +static void string_test_strnlen(struct kunit *test) +{ + char *s; + size_t len, offset; + const size_t buf_size =3D 16 + 128 + 1; /* max_offset + max_len + '\0' */ + + s =3D kunit_kzalloc(test, buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, s); + + memset(s, 'A', buf_size); + s[buf_size - 1] =3D '\0'; + + for (offset =3D 0; offset < 16; offset++) { + for (len =3D 0; len <=3D 128; len++) { + s[offset + len] =3D '\0'; + + if (len > 0) + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len - 1), len - 1); + + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len), len); + + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 1), len); + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 10), len); + + s[offset + len] =3D 'A'; + } + } +} + static void string_test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -641,6 +670,7 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset32), KUNIT_CASE(string_test_memset64), KUNIT_CASE(string_test_strlen), + KUNIT_CASE(string_test_strnlen), KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strspn), --=20 2.25.1