]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_strncpy.c: Remove strncpy_a() tests
authorAlejandro Colomar <alx@kernel.org>
Fri, 21 Feb 2025 09:22:36 +0000 (10:22 +0100)
committerSerge Hallyn <serge@hallyn.com>
Wed, 15 Jul 2026 16:11:00 +0000 (11:11 -0500)
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: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122963>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
tests/unit/Makefile.am
tests/unit/test_strncpy.c [deleted file]

index 0eb14849fce361cddc6a1a30ab4b13ffc9fa6d58..270de852da29e17206ffd37a37fdfce8bbeec372 100644 (file)
@@ -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 (file)
index e20a08d..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-
-#include "config.h"
-
-#include <string.h>
-
-#include <stdarg.h>  // Required by <cmocka.h>
-#include <stddef.h>  // Required by <cmocka.h>
-#include <setjmp.h>  // Required by <cmocka.h>
-#include <stdint.h>  // Required by <cmocka.h>
-#include <cmocka.h>
-
-#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);
-}