Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z2 #862

Merged
merged 2 commits into from
Mar 11, 2024
Merged

Z2 #862

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ libshadow_la_SOURCES = \
string/strncpy.h \
string/strtcpy.c \
string/strtcpy.h \
string/zustr2stp.c \
string/zustr2stp.h \
strtoday.c \
sub.c \
Expand Down
14 changes: 2 additions & 12 deletions lib/string/zustr2stp.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <[email protected]>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include <stddef.h>

#ident "$Id$"

#include "string/zustr2stp.h"


extern inline char *zustr2stp(char *restrict dst, const char *restrict src,
size_t sz);
61 changes: 20 additions & 41 deletions lib/string/zustr2stp.h
Original file line number Diff line number Diff line change
@@ -1,79 +1,58 @@
/*
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <[email protected]>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
#define SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_ZUSTR2STP_H_
#define SHADOW_INCLUDE_LIB_STRING_ZUSTR2STP_H_


#include <config.h>

#include <assert.h>
#include <stddef.h>
#include <string.h>

#include "must_be.h"
#include "sizeof.h"


#define ZUSTR2STP(dst, src) \
({ \
static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
\
zustr2stp(dst, src, NITEMS(src)); \
})


inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz);


/*
* SYNOPSIS
* char *zustr2stp(char *restrict dst,
* const char src[restrict .sz], size_t sz);
* char *ZUSTR2STP(char *restrict dst, const char src[restrict]);
*
* ARGUMENTS
* dst Destination buffer where to copy a string.
*
* src Source null-padded character sequence to be copied into
* dst.
*
* sz Size of the *source* buffer.
* dst Destination buffer.
* src Source null-padded character sequence.
*
* DESCRIPTION
* This function copies the null-padded character sequence pointed
* to by src, into a string at the buffer pointed to by dst.
* This macro copies at most NITEMS(src) non-null bytes from the
* array pointed to by src, followed by a null character, to the
* buffer pointed to by dst.
*
* RETURN VALUE
* dst + strlen(dst)
* This function returns a pointer to the terminating NUL
* byte.
*
* ERRORS
* This function doesn't set errno.
*
* CAVEATS
* This function doesn't know the size of the destination buffer.
* It assumes it will always be large enough. Since the size of
* the source buffer is known to the caller, it should make sure to
* allocate a destination buffer of at least `sz + 1`.
* allocate a destination buffer of at least `NITEMS(src) + 1`.
*
* EXAMPLES
* char src[13] = "Hello, world!" // No '\0' in this buffer!
* char dst[NITEMS(src) + 1];
* char hostname[NITEMS(utmp->ut_host) + 1];
*
* zustr2stp(dst, src, NITEMS(src));
* puts(dst);
* len = ZUSTR2STP(hostname, utmp->ut_host) - hostname;
* puts(hostname);
*/


inline char *
zustr2stp(char *restrict dst, const char *restrict src, size_t sz)
{
return stpcpy(mempcpy(dst, src, strnlen(src, sz)), "");
}
#define ZUSTR2STP(dst, src) \
({ \
static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
\
stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), ""); \
})


#endif // include guard
15 changes: 14 additions & 1 deletion tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ check_PROGRAMS = \
test_sprintf \
test_strncpy \
test_strtcpy \
test_xasprintf
test_xasprintf \
test_zustr2stp

if ENABLE_LOGIND
check_PROGRAMS += \
Expand Down Expand Up @@ -143,4 +144,16 @@ test_xasprintf_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)

test_zustr2stp_SOURCES = \
test_zustr2stp.c \
$(NULL)
test_zustr2stp_CFLAGS = \
$(AM_CFLAGS) \
$(NULL)
test_zustr2stp_LDFLAGS = \
$(NULL)
test_zustr2stp_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)

endif # HAVE_CMOCKA
53 changes: 53 additions & 0 deletions tests/unit/test_zustr2stp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include <stddef.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 "string/zustr2stp.h"


static void test_ZUSTR2STP(void **state);


int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_ZUSTR2STP),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}


static void
test_ZUSTR2STP(void **state)
{
char src[3] = {'1', '2', '3'};
char dst[4];

assert_true(ZUSTR2STP(&dst, src) == dst + strlen("123"));
assert_true(strcmp("123", dst) == 0);

src[2] = '\0';
assert_true(ZUSTR2STP(&dst, src) == dst + strlen("12"));
assert_true(strcmp("12", dst) == 0);

src[1] = '\0';
assert_true(ZUSTR2STP(&dst, src) == dst + strlen("1"));
assert_true(strcmp("1", dst) == 0);

src[0] = '\0';
assert_true(ZUSTR2STP(&dst, src) == dst + strlen(""));
assert_true(strcmp("", dst) == 0);
}
Loading