From nobody Tue Oct 15 11:40:59 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XSXGz4mvRz5YpZG; Tue, 15 Oct 2024 11:40:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XSXGz2QpWz4fkk; Tue, 15 Oct 2024 11:40:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728992459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YCT5RySDgx5OLrq1jvLPOUv6tufDAZNx00cQIUNcFe4=; b=EK90bhofuFz3Sqxc/3QTvyjC0xcAEM+yfpjQ4UOI9VxOqp4jorgbVKK/3rS65e5wvQnP0M 0SD9aoEWOooexHtqZhQGaYisyePKZoc8aiXeujTmdQeifbEnK8zLeyL3SEec8B+x84gFVf LEuBafdT1UldGq1UM4njCUq7B1E7yL37xsAnUR/BK9o+jNjqj2RCxY6hErpU/ImS2jBm+8 sXIufrCvq4GS4sz4l7bfnQy+I/ep5mN8/tKZWtmdCFojeQtFQc0sxPLbIr3xvi2hHpyQw9 HhC8D4ODjmbZ1j/B1w6ui5NPPGpV9c8FNF3iZA6e84fx39kLJWBbYZHexWRaUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728992459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YCT5RySDgx5OLrq1jvLPOUv6tufDAZNx00cQIUNcFe4=; b=n/gfWK0WcvhoxxeS4F8IekwdtbljH/bK0z5NkzLvjNVgJlaZ5czJ91qC/KIcfe6s91LqpA vIMxTWP3TLEuJTFlcqYKn7qSLhFcc4/W33w4G4UHctsi60clE9qp2xTg0Sa2/KpIMqvsI/ foy+QM/z/Bgy9y8ihMzmdBsZJIEZF+TBk+WKjvHwPVoZR1cdnITtjkUelCiEZeqlYTSqGc 5sieW3O3XxS3TpX6GJCZxPgIoO0CMRlDoK+qx+CGOd4lrsEE37+pTMjWZrMt1xTMmvbTrw /TIBIfwPMnhpr6xQcds35oVU+JYCXCJrWO3x4dCo1Fh5s2kkXeDfjXMikT2NfQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728992459; a=rsa-sha256; cv=none; b=iuFiqDq/oJpmHe8hMN8CHAH6cJetTHkTPwIxuDKsfKy7pt8Gy47CYbSMy7ZDZ+U4WFFYOn SlneOgs5R88q23cmt/7SJFfDiIR2hupK+DvJbdtVJ9kaZH+0AmYE2o0T1Eq9XWcZDDd7FX pi6fK8edEkAhTy/CtKu2pdzTggum6HxpWyyjbQeDTI6H+ob+dopPq+prXPpBws2G1Nf5vu u13LwB63GvXLzLLqaSKqdxQ9axkD/2UqV8hqWQ88qeb8B0Ap1FkrExaNyP3uTDmB+19aBx Gou/OmJGsK4Oi0LpISRrW8a21RVKmCcP6uAs5+9C2BLQxeENu0JmmDxIOTKUvA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XSXGz21ytzLCF; Tue, 15 Oct 2024 11:40:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49FBexK3043348; Tue, 15 Oct 2024 11:40:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49FBexw7043345; Tue, 15 Oct 2024 11:40:59 GMT (envelope-from git) Date: Tue, 15 Oct 2024 11:40:59 GMT Message-Id: <202410151140.49FBexw7043345@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 7bedae81adaf - main - lib/libc/tests/string: add test for strnlen() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7bedae81adaff4b4d291ba1f650b5b915ce207bf Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=7bedae81adaff4b4d291ba1f650b5b915ce207bf commit 7bedae81adaff4b4d291ba1f650b5b915ce207bf Author: Strahinja Stanišić AuthorDate: 2024-10-14 21:09:28 +0000 Commit: Robert Clausecker CommitDate: 2024-10-15 11:34:45 +0000 lib/libc/tests/string: add test for strnlen() Tests for strnlen, checks alignments from up to 16 and buffer sizes up to 64, also checks that passing SIZE_MAX as maxlen works, because it can cause a wraparound error if strnlen is incorrect. Reviewed by: fuz, emaste (GSoC mentors), kib Sponsored by: Google LLC (GSoC 2024) Differential Revision: https://reviews.freebsd.org/D46275 --- lib/libc/tests/string/Makefile | 1 + lib/libc/tests/string/strnlen_test.c | 141 +++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/lib/libc/tests/string/Makefile b/lib/libc/tests/string/Makefile index f46076619352..a019939c30af 100644 --- a/lib/libc/tests/string/Makefile +++ b/lib/libc/tests/string/Makefile @@ -15,6 +15,7 @@ ATF_TESTS_C+= memset2_test ATF_TESTS_C+= memset_s_test ATF_TESTS_C+= strncmp_test ATF_TESTS_C+= stpncpy_test +ATF_TESTS_C+= strnlen_test ATF_TESTS_C+= strcmp2_test ATF_TESTS_C+= strcspn_test ATF_TESTS_C+= strerror2_test diff --git a/lib/libc/tests/string/strnlen_test.c b/lib/libc/tests/string/strnlen_test.c new file mode 100644 index 000000000000..31c2384bb30f --- /dev/null +++ b/lib/libc/tests/string/strnlen_test.c @@ -0,0 +1,141 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic + */ + +#include +#include +#include +#include +#include +#include + +#include + +ATF_TC(strnlen_alignments); +ATF_TC_HEAD(strnlen_alignments, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test strnlen(3) with different alignments"); +} + +ATF_TC_BODY(strnlen_alignments, tc) +{ + size_t (*strnlen_fn)(const char*, size_t) = strnlen; + char alignas(16) buffer[1 + 16 + 64 + 1 + 1]; + + memset(buffer, '/', sizeof(buffer)); + + for (int align = 1; align < 1 + 16; align++) { + char *s = buffer + align; + + for (size_t maxlen = 0; maxlen <= 64; maxlen++) { + for (size_t len = 0; len <= maxlen; len++) { + /* returns length */ + + /* without sentinels */ + s[len] = '\0'; + size_t val = strnlen_fn(s, maxlen); + if (val != len) { + fprintf(stderr, "align = %d, maxlen = %zu, len = %zu", + align, maxlen, len); + atf_tc_fail("returned incorrect len"); + } + + /* with sentinels */ + s[-1] = '\0'; + s[maxlen + 1] = '\0'; + val = strnlen_fn(s, maxlen); + if (val != len) { + fprintf(stderr, "align = %d, maxlen = %zu, len = %zu", + align, maxlen, len); + atf_tc_fail("returned incorrect len (sentinels)"); + } + + /* cleanup */ + s[-1] = '/'; + s[len] = '/'; + s[maxlen + 1] = '/'; + + } + + /* returns maxlen */ + + /* without sentinels */ + size_t val = strnlen_fn(s, maxlen); + if (val != maxlen) { + fprintf(stderr, "align = %d, maxlen = %zu", + align, maxlen); + atf_tc_fail("should return maxlen"); + } + + /* with sentinels */ + s[-1] = '\0'; + s[maxlen + 1] = '\0'; + val = strnlen_fn(s, maxlen); + if (val != maxlen) { + fprintf(stderr, "align = %d, maxlen = %zu", + align, maxlen); + atf_tc_fail("should return maxlen (sentinels)"); + } + + /* cleanup */ + s[-1] = '/'; + s[maxlen + 1] = '/'; + } + } +} + +ATF_TC(strnlen_size_max); +ATF_TC_HEAD(strnlen_size_max, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test strnlen(3) with maxlen=SIZE_MAX"); +} + +ATF_TC_BODY(strnlen_size_max, tc) +{ + size_t (*strnlen_fn)(const char*, size_t) = strnlen; + char alignas(16) buffer[1 + 16 + 64 + 1 + 1]; + + memset(buffer, '/', sizeof(buffer)); + + for (int align = 1; align < 1 + 16; align++) { + char* s = buffer + align; + + for (size_t len = 0; len <= 64; len++) { + /* returns length */ + + /* without sentinels */ + s[len] = '\0'; + size_t val = strnlen_fn(s, SIZE_MAX); + if (val != len) { + fprintf(stderr, "align = %d, maxlen = %zu, len = %zu", + align, SIZE_MAX, len); + atf_tc_fail("returned incorrect len (SIZE_MAX)"); + } + + /* with sentinels */ + s[-1] = '\0'; + val = strnlen_fn(s, SIZE_MAX); + if (val != len) { + fprintf(stderr, "align = %d, maxlen = %zu, len = %zu", + align, SIZE_MAX, len); + atf_tc_fail("returned incorrect len (sentinels) (SIZE_MAX)"); + } + + /* cleanup */ + s[-1] = '/'; + s[len] = '/'; + } + } +} + + + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, strnlen_alignments); + ATF_TP_ADD_TC(tp, strnlen_size_max); + + return atf_no_error(); +}