From f9f3bb77618267d94cc2e5102c35e9d036cfcde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ni=C3=B1o=20D=C3=ADaz?= Date: Sat, 27 Jan 2018 00:02:26 +0000 Subject: [PATCH] Remove dependency of strlcat() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was only one place where `strlcat` was used, and `snprintf` actually does a better job at what the code was trying to achieve. Signed-off-by: Antonio Niño Díaz --- LICENSE.rst | 4 ++-- Makefile | 1 - include/extern/strl.h | 2 -- src/asm/fstack.c | 17 ++++++++++---- src/extern/strlcat.c | 54 ------------------------------------------- 5 files changed, 15 insertions(+), 63 deletions(-) delete mode 100644 src/extern/strlcat.c diff --git a/LICENSE.rst b/LICENSE.rst index afb00a43..91d3a42f 100644 --- a/LICENSE.rst +++ b/LICENSE.rst @@ -27,5 +27,5 @@ Other extern/reallocarray.c is derived from the OpenBSD Project, http://www.openbsd.org, and is released under the ISC license. -extern/strlcat.c and extern/strlcpy.c are derived from the OpenBSD Project, -http://www.openbsd.org, and are released under the BSD license. +extern/strlcpy.c is derived from the OpenBSD Project, http://www.openbsd.org, +and is released under the BSD license. diff --git a/Makefile b/Makefile index 00a13623..5c740ac2 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,6 @@ rgbasm_obj := \ src/extern/err.o \ src/extern/reallocarray.o \ src/extern/strlcpy.o \ - src/extern/strlcat.o \ src/extern/utf8decoder.o \ src/extern/version.o diff --git a/include/extern/strl.h b/include/extern/strl.h index ac16c3fd..ffcb7570 100644 --- a/include/extern/strl.h +++ b/include/extern/strl.h @@ -16,9 +16,7 @@ #else /* STRL_IN_LIBC */ #define strlcpy rgbds_strlcpy -#define strlcat rgbds_strlcat size_t strlcpy(char *dst, const char *src, size_t dsize); -size_t strlcat(char *dst, const char *src, size_t dsize); #endif /* STRL_IN_LIBC */ diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 72eb6c79..df2f2f32 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -236,6 +236,9 @@ FILE *fstk_FindFile(char *fname) int32_t i; FILE *f; + if (fname == NULL) + return NULL; + f = fopen(fname, "rb"); if (f != NULL || errno != ENOENT) { @@ -246,13 +249,19 @@ FILE *fstk_FindFile(char *fname) } for (i = 0; i < NextIncPath; ++i) { - if (strlcpy(path, IncludePaths[i], sizeof(path)) + /* + * The function snprintf() does not write more than `size` bytes + * (including the terminating null byte ('\0')). If the output + * was truncated due to this limit, the return value is the + * number of characters (excluding the terminating null byte) + * which would have been written to the final string if enough + * space had been available. Thus, a return value of `size` or + * more means that the output was truncated. + */ + if (snprintf(path, sizeof(path), "%s%s", IncludePaths[i], fname) >= sizeof(path)) continue; - if (strlcat(path, fname, sizeof(path)) >= sizeof(path)) - continue; - f = fopen(path, "rb"); if (f != NULL || errno != ENOENT) { diff --git a/src/extern/strlcat.c b/src/extern/strlcat.c deleted file mode 100644 index 4f4ae8ce..00000000 --- a/src/extern/strlcat.c +++ /dev/null @@ -1,54 +0,0 @@ -/* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */ - -/* - * Copyright (c) 1998, 2015 Todd C. Miller - * - * 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 -#include - -/* - * Appends src to string dst of size dsize (unlike strncat, dsize is the - * full size of dst, not space left). At most dsize-1 characters - * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). - * Returns strlen(src) + MIN(dsize, strlen(initial dst)). - * If retval >= dsize, truncation occurred. - */ -size_t rgbds_strlcat(char *dst, const char *src, size_t dsize) -{ - const char *odst = dst; - const char *osrc = src; - size_t n = dsize; - size_t dlen; - - /* Find the end of dst and adjust bytes left but don't go past end. */ - while (n-- != 0 && *dst != '\0') - dst++; - dlen = dst - odst; - n = dsize - dlen; - - if (n-- == 0) - return(dlen + strlen(src)); - while (*src != '\0') { - if (n != 0) { - *dst++ = *src; - n--; - } - src++; - } - *dst = '\0'; - - return dlen + (src - osrc); /* count does not include NUL */ -}