blob: d94fd3290e4b5fc5faf032298073208198c9a4b1 [file] [log] [blame]
Andrew Geissler82c905d2020-04-13 13:39:40 -05001From 8a204171004fa0d7d21389530c744d215e99efb0 Mon Sep 17 00:00:00 2001
2From: Joshua Watt <JPEWhacker@gmail.com>
3Date: Tue, 19 Nov 2019 12:47:30 -0600
4Subject: [PATCH 1/2] stdlib: Add strlcat
5
6Adds strlcat which can be used to safely concatenate strings
7
8Upstream-Status: Submitted [https://bugzilla.nasm.us/show_bug.cgi?id=3392635]
9Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
10---
11 Makefile.in | 2 +-
12 configure.ac | 2 ++
13 include/compiler.h | 4 ++++
14 stdlib/strlcat.c | 43 +++++++++++++++++++++++++++++++++++++++++++
15 4 files changed, 50 insertions(+), 1 deletion(-)
16 create mode 100644 stdlib/strlcat.c
17
18diff --git a/Makefile.in b/Makefile.in
19index 32ef3d91..ff7eb447 100644
20--- a/Makefile.in
21+++ b/Makefile.in
22@@ -93,7 +93,7 @@ NASM = asm/nasm.$(O)
23 NDISASM = disasm/ndisasm.$(O)
24
25 LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \
26- stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) \
27+ stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) stdlib/strlcat.$(O) \
28 \
29 nasmlib/ver.$(O) \
30 nasmlib/crc64.$(O) nasmlib/malloc.$(O) nasmlib/errfile.$(O) \
31diff --git a/configure.ac b/configure.ac
32index 38b3b596..b4e88778 100644
33--- a/configure.ac
34+++ b/configure.ac
35@@ -152,6 +152,7 @@ AC_CHECK_FUNCS([vsnprintf _vsnprintf])
36 AC_CHECK_FUNCS([snprintf _snprintf])
37 AC_CHECK_FUNCS([strlcpy])
38 AC_CHECK_FUNCS([strrchrnul])
39+AC_CHECK_FUNCS([strlcat])
40
41 dnl These types are POSIX-specific, and Windows does it differently...
42 AC_CHECK_TYPES([struct _stati64])
43@@ -170,6 +171,7 @@ AC_CHECK_DECLS(strsep)
44 AC_CHECK_DECLS(strlcpy)
45 AC_CHECK_DECLS(strnlen)
46 AC_CHECK_DECLS(strrchrnul)
47+AC_CHECK_DECLS(strlcat)
48
49 dnl Check for missing types
50 AC_TYPE_UINTPTR_T
51diff --git a/include/compiler.h b/include/compiler.h
52index 4178c98e..8153d297 100644
53--- a/include/compiler.h
54+++ b/include/compiler.h
55@@ -159,6 +159,10 @@ size_t strlcpy(char *, const char *, size_t);
56 char *strrchrnul(const char *, int);
57 #endif
58
59+#if !defined(HAVE_STRLCAT) || !HAVE_DECL_STRLCAT
60+size_t strlcat(char *, const char *, size_t);
61+#endif
62+
63 #ifndef __cplusplus /* C++ has false, true, bool as keywords */
64 # ifdef HAVE_STDBOOL_H
65 # include <stdbool.h>
66diff --git a/stdlib/strlcat.c b/stdlib/strlcat.c
67new file mode 100644
68index 00000000..7084d460
69--- /dev/null
70+++ b/stdlib/strlcat.c
71@@ -0,0 +1,43 @@
72+/*
73+ * Copyright (c) 2019 Garmin Ltd. or its subsidiaries
74+ *
75+ * Permission to use, copy, modify, and distribute this software for any
76+ * purpose with or without fee is hereby granted, provided that the above
77+ * copyright notice and this permission notice appear in all copies.
78+ *
79+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
80+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
81+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
82+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
83+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
84+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
85+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
86+ */
87+
88+#include "compiler.h"
89+
90+/*
91+ * Concatenate src string to dest of size size. The destination buffer will
92+ * have no more than size-1 character when the operation finishes. Always NUL
93+ * terminates, unless size == 0 or dest has no NUL terminator. Returns
94+ * strlen(initial dest) + strlen(src); if retval >= size, truncation occurred.
95+ */
96+#ifndef HAVE_STRLCAT
97+
98+size_t strlcat(char *dest, const char *src, size_t size)
99+{
100+ size_t n;
101+
102+ /* find the NULL terminator in dest */
103+ for (n = 0; i < size && dest[n] != '\0'; n++)
104+ ;
105+
106+ /* destination was not NULL terminated. Return the initial size */
107+ if (n == size)
108+ return size;
109+
110+ return strlcpy(&dest[n], src, size - n) + n;
111+}
112+
113+#endif
114+
115--
1162.23.0
117