*/: s/STRNCPY/strncpy_a/

This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2025-10-14 12:54:04 +02:00 committed by Serge Hallyn
parent 4ddc73dbaf
commit d13bb54184
5 changed files with 26 additions and 25 deletions

View File

@ -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)) {

View File

@ -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.

View File

@ -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

View File

@ -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),

View File

@ -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);
}