Skip to content
Draft
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
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ libshadow_la_SOURCES = \
string/strcpy/strncat.h \
string/strcpy/strncpy.c \
string/strcpy/strncpy.h \
string/strcpy/strtcat.c \
string/strcpy/strtcat.h \
string/strcpy/strtcpy.c \
string/strcpy/strtcpy.h \
string/strdup/strdup.c \
Expand Down
7 changes: 2 additions & 5 deletions lib/salt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "prototypes.h"
#include "shadowlog.h"
#include "string/strcmp/streq.h"
#include "string/strcpy/strtcat.h"


#if (defined CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY && \
Expand Down Expand Up @@ -443,12 +444,8 @@ static /*@observer@*/const char *gensalt (size_t salt_size)

return retval;
#else /* USE_XCRYPT_GENSALT */
/* Check if the result buffer is long enough. */
assert (GENSALT_SETTING_SIZE > strlen (result) + salt_len);

/* Concatenate a pseudo random salt. */
strncat (result, gensalt (salt_len),
GENSALT_SETTING_SIZE - strlen (result) - 1);
assert(strtcat_a(result, gensalt(salt_len)) != -1);

return result;
#endif /* USE_XCRYPT_GENSALT */
Expand Down
4 changes: 2 additions & 2 deletions lib/string/README
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ strcpy/ - String copying
STRTCPY()
Like strtcpy(), but takes an array.

strtcat() // Unimplemented
strtcat()
Catenate a string after an existing string, with truncation.
Rarely useful. Consider using stpecpy() instead.
STRTCAT() // Unimplemented
strtcat_a()
Like strtcat(), but takes an array.

stpecpy()
Expand Down
13 changes: 13 additions & 0 deletions lib/string/strcpy/strtcat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "string/strcpy/strtcat.h"

#include <stddef.h>


extern inline ssize_t strtcat(char *restrict dst, const char *restrict src,
size_t dsize);
45 changes: 45 additions & 0 deletions lib/string/strcpy/strtcat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCAT_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCAT_H_


#include "config.h"

#include <stddef.h>

#include "attr.h"
#include "sizeof.h"
#include "string/strchr/strnul.h"
#include "string/strcpy/stpecpy.h"


// strtcat_a - string truncate catenate array
#define strtcat_a(dst, src) strtcat(dst, src, countof(dst))


ATTR_STRING(2)
inline ssize_t strtcat(char *restrict dst, const char *restrict src,
size_t dsize);


// strtcat - string truncate catenate
// Returns new length, or -1 on truncation.
inline ssize_t
strtcat(char *restrict dst, const char *restrict src, size_t dsize)
{
char *p, *end;

end = dst + dsize;

p = stpecpy(strnul(dst), end, src);
if (p == NULL)
return -1;

return p - dst;
}


#endif // include guard
Loading