commit feadc201286680fd24ff628d73ed94b2c156e43f
parent 3fa604b57f2245aabc92cf35db177ce0ad00c284
Author: Richard Ipsum <richardipsum@vx21.xyz>
Date: Mon, 5 Apr 2021 15:39:04 +0100
Replace strncpy with strlcpy
and remove some VLAs.
Diffstat:
M | Makefile | | | 6 | +++--- |
A | strlcpy.c | | | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
M | tyarn.c | | | 42 | +++++++++++++++++++++++++++++++----------- |
3 files changed, 82 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -16,8 +16,8 @@ all: tyarn tyarn.so
tyarn: tyarn.lua.in
sed 's|##LUA_INTERP##|$(LUA_INTERP)|' tyarn.lua.in > $@
-tyarn.so: tyarn.o
- $(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ tyarn.o
+tyarn.so: tyarn.o strlcpy.o
+ $(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ tyarn.o strlcpy.o
tyarn.o: tyarn.c
$(CC) -c -fPIC $(CFLAGS) -o $@ tyarn.c
@@ -33,6 +33,6 @@ uninstall: all
rm -f $(DESTDIR)$(LUA_CMOD_INST)/tyarn.so
clean:
- rm -f tyarn tyarn.o tyarn.so
+ rm -f tyarn *.o tyarn.so
.PHONY: uninstall install clean all
diff --git a/strlcpy.c b/strlcpy.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+/*
+ * Copy src to string dst of size siz. At most siz-1 characters
+ * will be copied. Always NUL terminates (unless siz == 0).
+ * Returns strlen(src); if retval >= siz, truncation occurred.
+ */
+size_t
+strlcpy(char *dst, const char *src, size_t siz)
+{
+ char *d = dst;
+ const char *s = src;
+ size_t n = siz;
+ /* Copy as many bytes as will fit */
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*d++ = *s++) == '\0')
+ break;
+ }
+ }
+ /* Not enough room in dst, add NUL and traverse rest of src */
+ if (n == 0) {
+ if (siz != 0)
+ *d = '\0'; /* NUL-terminate dst */
+ while (*s++)
+ ;
+ }
+ return(s - src - 1); /* count does not include NUL */
+}
diff --git a/tyarn.c b/tyarn.c
@@ -43,6 +43,8 @@
#include <sys/select.h>
#include <regex.h>
+size_t strlcpy(char *, const char *, size_t);
+
struct pipe_state {
int fd;
char *p;
@@ -132,13 +134,18 @@ static int tyarn_path_exists(lua_State *L)
static int tyarn_mkdtemp(lua_State *L)
{
- const char *path = luaL_checkstring(L, 1);
- char pathbuf[strlen(path) + 1];
+ const char *path;
+ char *pathbuf;
+ size_t pathbuf_size;
- strncpy(pathbuf, path, sizeof (pathbuf));
+ path = luaL_checkstring(L, 1);
+ pathbuf_size = strlen(path) + 1;
+ pathbuf = xmalloc(pathbuf_size);
+ strlcpy(pathbuf, path, pathbuf_size);
lua_pushstring(L, mkdtemp(pathbuf));
lua_pushinteger(L, errno);
+ free(pathbuf);
return 2;
}
@@ -542,14 +549,21 @@ static int tyarn_unlink(lua_State *L)
static int tyarn_mkstemp(lua_State *L)
{
- const char *path = luaL_checkstring(L, 1);
- char template[strlen(path) + 1];
- strncpy(template, path, sizeof (template));
- int fd = mkstemp(template);
+ const char *path;
+ char *template;
+ int fd;
+ size_t template_size;
+
+ path = luaL_checkstring(L, 1);
+ template_size = strlen(path) + 1;
+ template = xmalloc(template_size);
+ strlcpy(template, path, template_size);
+ fd = mkstemp(template);
if (fd == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
+ free(template);
return 2;
}
@@ -559,19 +573,25 @@ static int tyarn_mkstemp(lua_State *L)
*/
lua_pushstring(L, template);
+ free(template);
return 1;
}
static char *str_from_offs(const char *orig, regoff_t start, regoff_t end)
{
- char buf[strlen(orig) + 1];
- char *p;
- strncpy(buf, orig, sizeof (buf));
+ char *p, *buf, *newstr;
+ size_t bufsiz;
+
+ bufsiz = strlen(orig) + 1;
+ buf = xmalloc(bufsiz);
+ strlcpy(buf, orig, bufsiz);
p = buf + start;
buf[end] = '\0';
- return xstrdup(p);
+ newstr = xstrdup(p);
+ free(buf);
+ return newstr;
}
static int tyarn_rematch(lua_State *L)