From 1c5023002bad3a5b0bbc181fdb324160beace733 Mon Sep 17 00:00:00 2001 From: Joshua Watt Date: Tue, 19 Nov 2019 12:47:30 -0600 Subject: [PATCH] stdlib: Add strlcat Adds strlcat which can be used to safely concatenate strings Upstream-Status: Submitted [https://bugzilla.nasm.us/show_bug.cgi?id=3392635] Signed-off-by: Joshua Watt --- Makefile.in | 2 +- configure.ac | 2 ++ include/compiler.h | 4 ++++ stdlib/strlcat.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 stdlib/strlcat.c diff --git a/Makefile.in b/Makefile.in index bfae1f8..156dc4c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -101,7 +101,7 @@ NASM = asm/nasm.$(O) NDISASM = disasm/ndisasm.$(O) LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \ - stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) \ + stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) stdlib/strlcat.$(O) \ \ nasmlib/ver.$(O) \ nasmlib/alloc.$(O) nasmlib/asprintf.$(O) nasmlib/errfile.$(O) \ diff --git a/configure.ac b/configure.ac index 7b72769..14fd033 100644 --- a/configure.ac +++ b/configure.ac @@ -234,6 +234,7 @@ PA_FUNC_SNPRINTF PA_FUNC_VSNPRINTF AC_CHECK_FUNCS([strlcpy]) AC_CHECK_FUNCS([strrchrnul]) +AC_CHECK_FUNCS([strlcat]) dnl These types are POSIX-specific, and Windows does it differently... AC_CHECK_TYPES([struct _stati64]) @@ -253,6 +254,7 @@ AC_CHECK_DECLS(strsep) AC_CHECK_DECLS(strlcpy) AC_CHECK_DECLS(strnlen) AC_CHECK_DECLS(strrchrnul) +AC_CHECK_DECLS(strlcat) dnl Check for missing types AC_TYPE_UINTPTR_T diff --git a/include/compiler.h b/include/compiler.h index b4fd3a8..7fb4821 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -169,6 +169,10 @@ size_t strlcpy(char *, const char *, size_t); char *strrchrnul(const char *, int); #endif +#if !defined(HAVE_STRLCAT) || !HAVE_DECL_STRLCAT +size_t strlcat(char *, const char *, size_t); +#endif + #ifndef __cplusplus /* C++ has false, true, bool as keywords */ # ifdef HAVE_STDBOOL_H # include diff --git a/stdlib/strlcat.c b/stdlib/strlcat.c new file mode 100644 index 0000000..7084d46 --- /dev/null +++ b/stdlib/strlcat.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 Garmin Ltd. or its subsidiaries + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "compiler.h" + +/* + * Concatenate src string to dest of size size. The destination buffer will + * have no more than size-1 character when the operation finishes. Always NUL + * terminates, unless size == 0 or dest has no NUL terminator. Returns + * strlen(initial dest) + strlen(src); if retval >= size, truncation occurred. + */ +#ifndef HAVE_STRLCAT + +size_t strlcat(char *dest, const char *src, size_t size) +{ + size_t n; + + /* find the NULL terminator in dest */ + for (n = 0; i < size && dest[n] != '\0'; n++) + ; + + /* destination was not NULL terminated. Return the initial size */ + if (n == size) + return size; + + return strlcpy(&dest[n], src, size - n) + n; +} + +#endif +