From: Alejandro Colomar Date: Tue, 14 Oct 2025 10:54:04 +0000 (+0200) Subject: */: s/STRNCPY/strncpy_a/ X-Git-Tag: 4.19.0-rc1~71 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d13bb54184b238ad4d73ea61ba3211ce72a0528a;p=thirdparty%2Fshadow.git */: s/STRNCPY/strncpy_a/ This name better reflects that it handles arrays, and doesn't shout. Signed-off-by: Alejandro Colomar --- diff --git a/lib/log.c b/lib/log.c index fb09615ea..10da612b9 100644 --- a/lib/log.c +++ b/lib/log.c @@ -83,7 +83,7 @@ void dolastlog ( newlog.ll_time = ll_time; STRTCPY(newlog.ll_line, line); #if HAVE_LL_HOST - STRNCPY(newlog.ll_host, host); + strncpy_a(newlog.ll_host, host); #endif if ( (lseek (fd, offset, SEEK_SET) != offset) || (write_full(fd, &newlog, sizeof newlog) == -1)) { diff --git a/lib/string/README b/lib/string/README index 1123206b7..c2a7f2251 100644 --- a/lib/string/README +++ b/lib/string/README @@ -166,7 +166,7 @@ strdup/ - Memory duplication strcpy/ - String copying n/ - STRNCPY() + strncpy_a() Like strncpy(3), but takes an array. Use it *exclusively* for copying from a string into a utmp(5) member. diff --git a/lib/string/strcpy/strncpy.h b/lib/string/strcpy/strncpy.h index c9a5d0193..518e9674d 100644 --- a/lib/string/strcpy/strncpy.h +++ b/lib/string/strcpy/strncpy.h @@ -13,7 +13,8 @@ #include "sizeof.h" -#define STRNCPY(dst, src) strncpy(dst, src, countof(dst)) +// strncpy_a - nonstring copy array +#define strncpy_a(dst, src) strncpy(dst, src, countof(dst)) #endif // include guard diff --git a/lib/utmp.c b/lib/utmp.c index 8c1af1eef..baf29764b 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -289,21 +289,21 @@ prepare_utmp(const char *name, const char *line, const char *host, utent->ut_type = USER_PROCESS; utent->ut_pid = main_pid; - STRNCPY(utent->ut_line, line); + strncpy_a(utent->ut_line, line); if ( (NULL != ut) && ('\0' != ut->ut_id[0])) { - STRNCPY(utent->ut_id, ut->ut_id); + strncpy_a(utent->ut_id, ut->ut_id); } else { - STRNCPY(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id))); + strncpy_a(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id))); } #if defined(HAVE_STRUCT_UTMPX_UT_NAME) - STRNCPY(utent->ut_name, name); + strncpy_a(utent->ut_name, name); #endif - STRNCPY(utent->ut_user, name); + strncpy_a(utent->ut_user, name); if (NULL != hostname) { struct addrinfo *info = NULL; #if defined(HAVE_STRUCT_UTMPX_UT_HOST) - STRNCPY(utent->ut_host, hostname); + strncpy_a(utent->ut_host, hostname); #endif #if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN) utent->ut_syslen = MIN (strlen (hostname), diff --git a/tests/unit/test_strncpy.c b/tests/unit/test_strncpy.c index a6049ca58..e5655920c 100644 --- a/tests/unit/test_strncpy.c +++ b/tests/unit/test_strncpy.c @@ -18,18 +18,18 @@ #include "string/strcpy/strncpy.h" -static void test_STRNCPY_trunc(void **state); -static void test_STRNCPY_fit(void **state); -static void test_STRNCPY_pad(void **state); +static void test_strncpy_a_trunc(void **state); +static void test_strncpy_a_fit(void **state); +static void test_strncpy_a_pad(void **state); int main(void) { const struct CMUnitTest tests[] = { - cmocka_unit_test(test_STRNCPY_trunc), - cmocka_unit_test(test_STRNCPY_fit), - cmocka_unit_test(test_STRNCPY_pad), + 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); @@ -37,49 +37,49 @@ main(void) static void -test_STRNCPY_trunc(void **state) +test_strncpy_a_trunc(void **state) { char buf[3]; char src1[4] = {'f', 'o', 'o', 'o'}; char res1[3] = {'f', 'o', 'o'}; - assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0); + 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(buf, src2), sizeof(buf)) == 0); + assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0); } static void -test_STRNCPY_fit(void **state) +test_strncpy_a_fit(void **state) { char buf[3]; char src1[3] = {'b', 'a', 'z'}; char res1[3] = {'b', 'a', 'z'}; - assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0); + 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(buf, src2), sizeof(buf)) == 0); + assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0); } static void -test_STRNCPY_pad(void **state) +test_strncpy_a_pad(void **state) { char buf[3]; char src1[3] = "as"; char res1[3] = {'a', 's', 0}; - assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 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(buf, src2), sizeof(buf)) == 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(buf, src3), sizeof(buf)) == 0); + assert_true(memcmp(res3, strncpy_a(buf, src3), sizeof(buf)) == 0); }