From: Alejandro Colomar Date: Fri, 21 Feb 2025 09:22:36 +0000 (+0100) Subject: tests/unit/test_strncpy.c: Remove strncpy_a() tests X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f02b40c5cf45da15c4d38151051289c772fbf3a2;p=thirdparty%2Fshadow.git tests/unit/test_strncpy.c: Remove strncpy_a() tests It's just the obvious thin wrapper around strncpy(3); let's trust it's ok. The reason for removing this test is that GCC has bogus diagnostics for strncpy(3). Its diagnostics are geared towards helping people that abuse strncpy(3) as a poor-man's strlcpy(3) not write exploitable code as easily. Using strncpy(3) for that purpose is brain damaged, and those programs should be audited to stop using this API for that. And most importantly, GCC should stop encouraging writing bad code that calls strncpy(3) as that results in diagnostics that are actively harmful for us, legitimate users of strncpy(3). Those false positives should certainly be out of -Wall. We could fill the tests with pragmas, but let's just remove the tests. I don't feel like maintaining code for ignoring GCC's brain bamage. If people want to write a function for truncating strings (because they can't rely on strlcpy(3) being available, or because they don't like it), they certainly should write such an API. strncpy(3) isn't that API. strncpy(3) is a function that takes a string and writes it into a utmpx(5) member, which is NOT a string. (And I heard it might also be useful for implementing tar(1), which also uses non-terminated character arrays, but I never looked at that code.) Link: Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 0eb14849f..270de852d 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -11,7 +11,6 @@ check_PROGRAMS = \ test_chkhash \ test_chkname \ test_stprintf \ - test_strncpy \ test_strtcpy \ test_typetraits \ test_exit_if_null @@ -107,19 +106,6 @@ test_stprintf_LDADD = \ $(CMOCKA_LIBS) \ $(NULL) -test_strncpy_SOURCES = \ - test_strncpy.c \ - $(NULL) -test_strncpy_CFLAGS = \ - $(AM_CFLAGS) \ - $(NULL) -test_strncpy_LDFLAGS = \ - $(NULL) -test_strncpy_LDADD = \ - $(LIBSHADOW) \ - $(CMOCKA_LIBS) \ - $(NULL) - test_strtcpy_SOURCES = \ test_strtcpy.c \ $(NULL) diff --git a/tests/unit/test_strncpy.c b/tests/unit/test_strncpy.c deleted file mode 100644 index e20a08d72..000000000 --- a/tests/unit/test_strncpy.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023, Alejandro Colomar - * SPDX-License-Identifier: BSD-3-Clause - */ - - -#include "config.h" - -#include - -#include // Required by -#include // Required by -#include // Required by -#include // Required by -#include - -#include "sizeof.h" -#include "string/strcpy/strncpy.h" -#include "attr.h" - -static void test_strncpy_a_trunc(MAYBE_UNUSED void ** _1); -static void test_strncpy_a_fit(MAYBE_UNUSED void ** _1); -static void test_strncpy_a_pad(MAYBE_UNUSED void ** _1); - - -int -main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_strncpy_a_trunc), - cmocka_unit_test(test_strncpy_a_fit), - cmocka_unit_test(test_strncpy_a_pad), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} - - -static void -test_strncpy_a_trunc(MAYBE_UNUSED void ** _1) -{ - char buf[3]; - - char src1[4] = {'f', 'o', 'o', 'o'}; - char res1[3] = {'f', 'o', 'o'}; - assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0); - - char src2[5] = "barb"; - char res2[3] = {'b', 'a', 'r'}; - assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0); -} - - -static void -test_strncpy_a_fit(MAYBE_UNUSED void ** _1) -{ - char buf[3]; - - char src1[3] = {'b', 'a', 'z'}; - char res1[3] = {'b', 'a', 'z'}; - assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0); - - char src2[4] = "qwe"; - char res2[3] = {'q', 'w', 'e'}; - assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0); -} - - -static void -test_strncpy_a_pad(MAYBE_UNUSED void ** _1) -{ - char buf[3]; - - char src1[3] = "as"; - char res1[3] = {'a', 's', 0}; - assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0); - - char src2[3] = ""; - char res2[3] = {0, 0, 0}; - assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0); - - char src3[3] = {'a', 0, 'b'}; - char res3[3] = {'a', 0, 0}; - assert_true(memcmp(res3, strncpy_a(buf, src3), sizeof(buf)) == 0); -}