From a78f254611ae9889928af07766b946b97e25ade5 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 19 Oct 2018 12:28:04 -0700 Subject: [PATCH] GUACAMOLE-637: Add strlcat() implementation. Use libc strlcat() if available. --- configure.ac | 5 +++++ src/libguac/guacamole/string.h | 39 ++++++++++++++++++++++++++++++++++ src/libguac/string.c | 11 ++++++++++ 3 files changed, 55 insertions(+) diff --git a/configure.ac b/configure.ac index c3ea7366..19396204 100644 --- a/configure.ac +++ b/configure.ac @@ -125,6 +125,11 @@ AC_CHECK_DECL([strlcpy], [Whether strlcpy() is defined])],, [#include ]) +AC_CHECK_DECL([strlcat], + [AC_DEFINE([HAVE_STRLCAT],, + [Whether strlcat() is defined])],, + [#include ]) + # Typedefs AC_TYPE_SIZE_T AC_TYPE_SSIZE_T diff --git a/src/libguac/guacamole/string.h b/src/libguac/guacamole/string.h index 89561ab0..b3a89c9c 100644 --- a/src/libguac/guacamole/string.h +++ b/src/libguac/guacamole/string.h @@ -64,5 +64,44 @@ */ size_t guac_strlcpy(char* restrict dest, const char* restrict src, size_t n); +/** + * Appends the given source string after the end of the given destination + * string, writing at most the given number of bytes. Both the source and + * destination strings MUST be null-terminated. The resulting buffer will + * always be null-terminated, even if doing so means that the intended string + * is truncated, unless the destination buffer has no space available at all. + * As this function always returns the length of the string it tried to create + * (the length of destination and source strings added together), whether + * truncation has occurred can be detected by comparing the return value + * against the size of the destination buffer. If the value returned is greater + * than or equal to the size of the destination buffer, then the string has + * been truncated. + * + * The source and destination buffers MAY NOT overlap. + * + * @param dest + * The buffer which should be appended with the contents of the source + * string. This buffer MUST already be null-terminated and will always be + * null-terminated unless zero bytes are available within the buffer. + * + * @param src + * The source string to append to the the destination buffer. This string + * MUST be null-terminated. + * + * @param n + * The number of bytes available within the destination buffer. If this + * value is not greater than zero, no bytes will be written to the + * destination buffer, and the destination buffer may not be + * null-terminated. In all other cases, the destination buffer will always + * be null-terminated, even if doing so means that the copied data from the + * source string will be truncated. + * + * @return + * The length of the string this function tried to create (the lengths of + * the source and destination strings added together) in bytes, excluding + * the null terminator. + */ +size_t guac_strlcat(char* restrict dest, const char* restrict src, size_t n); + #endif diff --git a/src/libguac/string.c b/src/libguac/string.c index fbcbcf59..879c5267 100644 --- a/src/libguac/string.c +++ b/src/libguac/string.c @@ -50,3 +50,14 @@ size_t guac_strlcpy(char* restrict dest, const char* restrict src, size_t n) { } +size_t guac_strlcat(char* restrict dest, const char* restrict src, size_t n) { + +#ifdef HAVE_STRLCPY + return strlcat(dest, src, n); +#else + int length = strnlen(dest, n); + return length + guac_strlcpy(dest + length, src, n - length); +#endif + +} +