From 25da758257ec0f324642f6c44dd2a524d9f14ec8 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 18 Dec 2020 15:50:51 -0500 Subject: [PATCH] Implement patty_strlcpy() Implement patty_strlcpy() for portability and correctness, to provide BSD strlcpy() functionality for glibc systems while ensuring a nul terminated string is written to a destination, truncating the source string by one byte if need be to make room for the nul terminator --- bin/ax25dump.c | 3 ++- bin/tncd.c | 5 +++-- include/patty/util.h | 8 ++++++++ src/Makefile | 4 ++-- src/client.c | 7 ++++--- src/server.c | 7 ++++--- src/tnc.c | 3 ++- src/util.c | 17 +++++++++++++++++ 8 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 include/patty/util.h create mode 100644 src/util.c diff --git a/bin/ax25dump.c b/bin/ax25dump.c index 8f35455..007940c 100644 --- a/bin/ax25dump.c +++ b/bin/ax25dump.c @@ -13,6 +13,7 @@ #include #include +#include #include @@ -91,7 +92,7 @@ int main(int argc, char **argv) { goto error_client_socket; } - strncpy(ifreq.name, ifname, sizeof(ifreq.name)-1); + patty_strlcpy(ifreq.name, ifname, sizeof(ifreq.name)); ifreq.state = PATTY_AX25_SOCK_PROMISC; diff --git a/bin/tncd.c b/bin/tncd.c index 080bed5..f1b5379 100644 --- a/bin/tncd.c +++ b/bin/tncd.c @@ -14,6 +14,7 @@ #include #include +#include #include @@ -108,7 +109,7 @@ static int pty_promisc(patty_client *client, setsockopt_request.opt = PATTY_AX25_SOCK_IF; setsockopt_request.len = sizeof(ifreq); - strncpy(ifreq.name, ifname, sizeof(ifreq.name)-1); + patty_strlcpy(ifreq.name, ifname, sizeof(ifreq.name)); ifreq.state = PATTY_AX25_SOCK_PROMISC; call = PATTY_CLIENT_SETSOCKOPT; @@ -141,7 +142,7 @@ static int pty_promisc(patty_client *client, goto error_client_setsockopt; } - strncpy(pty, socket_response.path, len); + patty_strlcpy(pty, socket_response.path, len); return socket_response.fd; diff --git a/include/patty/util.h b/include/patty/util.h new file mode 100644 index 0000000..53ce1b0 --- /dev/null +++ b/include/patty/util.h @@ -0,0 +1,8 @@ +#ifndef _PATTY_UTIL_H +#define _PATTY_UTIL_H + +#include + +ssize_t patty_strlcpy(char *dest, const char *src, size_t n); + +#endif /* _PATTY_UTIL_H */ diff --git a/src/Makefile b/src/Makefile index 0144442..3a204e2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,11 +9,11 @@ LDFLAGS += -lutil HEADERS = kiss.h kiss/tnc.h ax25/aprs_is.h ax25.h client.h ax25/if.h \ ax25/frame.h ax25/sock.h ax25/route.h ax25/server.h daemon.h \ - error.h list.h hash.h dict.h timer.h print.h conf.h + error.h list.h hash.h dict.h timer.h print.h util.h conf.h OBJS = kiss.o tnc.o aprs_is.o ax25.o client.o if.o \ frame.o sock.o route.o server.o daemon.o \ - error.o list.o hash.o dict.o timer.o print.o conf.o + error.o list.o hash.o dict.o timer.o print.o util.o conf.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/client.c b/src/client.c index 5db87cb..c20289e 100644 --- a/src/client.c +++ b/src/client.c @@ -10,6 +10,7 @@ #include #include +#include struct _patty_client_sock { int fd; @@ -49,7 +50,7 @@ patty_client *patty_client_new(const char *path) { memset(&addr, '\0', sizeof(addr)); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, find_sock(path), sizeof(addr.sun_path)-1); + patty_strlcpy(addr.sun_path, find_sock(path), sizeof(addr.sun_path)); if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { goto error_connect; @@ -190,7 +191,7 @@ int patty_client_socket(patty_client *client, sock->fd = response.fd; - strncpy(sock->path, response.path, sizeof(sock->path)); + patty_strlcpy(sock->path, response.path, sizeof(sock->path)); if ((fd = open(sock->path, O_RDWR)) < 0) { goto error_open; @@ -423,7 +424,7 @@ int patty_client_accept(patty_client *client, remote->fd = message.fd; memcpy(peer, &message.peer, sizeof(*peer)); - strncpy(remote->path, message.path, sizeof(remote->path)); + patty_strlcpy(remote->path, message.path, sizeof(remote->path)); if ((pty = open(remote->path, O_RDWR)) < 0) { goto error_open; diff --git a/src/server.c b/src/server.c index ebb9e73..de84d53 100644 --- a/src/server.c +++ b/src/server.c @@ -13,6 +13,7 @@ #include #include #include +#include typedef int (*patty_ax25_server_call)(patty_ax25_server *, int); @@ -458,7 +459,7 @@ int patty_ax25_server_if_add(patty_ax25_server *server, goto error_if_fd; } - strncpy(entry->name, name, sizeof(entry->name)-1); + patty_strlcpy(entry->name, name, sizeof(entry->name)); entry->iface = iface; @@ -598,7 +599,7 @@ static int notify_accept(int local, } if (path) { - strncpy(message.path, path, sizeof(message.path)-1); + patty_strlcpy(message.path, path, sizeof(message.path)); } return write(local, &message, sizeof(message)); @@ -1069,7 +1070,7 @@ static int listen_unix(patty_ax25_server *server, const char *path) { memset(&addr, '\0', sizeof(addr)); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1); + patty_strlcpy(addr.sun_path, path, sizeof(addr.sun_path)); if (bind(server->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { goto error_bind; diff --git a/src/tnc.c b/src/tnc.c index 026dcc9..f349b1e 100644 --- a/src/tnc.c +++ b/src/tnc.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "config.h" @@ -65,7 +66,7 @@ static int init_sock(patty_kiss_tnc *tnc, patty_kiss_tnc_info *info) { memset(&addr, '\0', sizeof(addr)); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, info->device, sizeof(addr.sun_path)-1); + patty_strlcpy(addr.sun_path, info->device, sizeof(addr.sun_path)); if (connect(tnc->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { goto error_connect; diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..ed768ef --- /dev/null +++ b/src/util.c @@ -0,0 +1,17 @@ +#include + +ssize_t patty_strlcpy(char *dest, const char *src, size_t n) { + size_t i; + + for (i=0; i