Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Oct 2024 11:40:59 GMT
From:      Robert Clausecker <fuz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 7bedae81adaf - main - lib/libc/tests/string: add test for strnlen()
Message-ID:  <202410151140.49FBexw7043345@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by fuz:

URL: https://cgit.FreeBSD.org/src/commit/?id=7bedae81adaff4b4d291ba1f650b5b915ce207bf

commit 7bedae81adaff4b4d291ba1f650b5b915ce207bf
Author:     Strahinja Stanišić <strajabot@FreeBSD.org>
AuthorDate: 2024-10-14 21:09:28 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
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 <strajabot@FreeBSD.org>
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdalign.h>
+#include <stdint.h>
+
+#include <atf-c.h>
+
+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();
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202410151140.49FBexw7043345>