diff --git a/lib/csv.c b/lib/csv.c index fdd89a0c7a99..2cd0092f0afc 100644 --- a/lib/csv.c +++ b/lib/csv.c @@ -573,7 +573,7 @@ void csv_decode(csv_t *csv, char *inbuf) log_error("field str malloc failed\n"); return; } - strncpy(rec->record, buf, pos - buf + 1); + memcpy(rec->record, buf, MIN(pos - buf + 1, csv->buflen - 1)); } rec->rec_len = pos - buf + 1; /* decode record into fields */ diff --git a/lib/ptm_lib.c b/lib/ptm_lib.c index 737c60775f64..a2ef045c7aa2 100644 --- a/lib/ptm_lib.c +++ b/lib/ptm_lib.c @@ -3,9 +3,7 @@ * Copyright (C) 2015 Cumulus Networks, Inc. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include #include #include @@ -450,7 +448,7 @@ ptm_lib_handle_t *ptm_lib_register(char *client_name, ptm_cmd_cb cmd_cb, hdl = calloc(1, sizeof(*hdl)); if (hdl) { - strncpy(hdl->client_name, client_name, PTMLIB_MAXNAMELEN - 1); + strlcpy(hdl->client_name, client_name, sizeof(hdl->client_name)); hdl->cmd_cb = cmd_cb; hdl->notify_cb = notify_cb; hdl->response_cb = response_cb; diff --git a/tests/helpers/c/prng.c b/tests/helpers/c/prng.c index 612c43344308..c72615475b5d 100644 --- a/tests/helpers/c/prng.c +++ b/tests/helpers/c/prng.c @@ -49,16 +49,17 @@ const char *prng_fuzz(struct prng *prng, const char *string, const char *charset, unsigned int operations) { static char buf[256]; - unsigned int charset_len; + size_t charset_len = strlen(charset); + size_t str_len = strlen(string); unsigned int i; unsigned int offset; unsigned int op; unsigned int character; - assert(strlen(string) < sizeof(buf)); + assert(str_len < sizeof(buf)); - strncpy(buf, string, sizeof(buf)); - charset_len = strlen(charset); + memset(buf, 0, sizeof(buf)); + memcpy(buf, string, str_len); for (i = 0; i < operations; i++) { offset = prng_rand(prng) % strlen(buf); diff --git a/tests/isisd/test_fuzz_isis_tlv.c b/tests/isisd/test_fuzz_isis_tlv.c index 627ccfee6fa3..9d460cd9c177 100644 --- a/tests/isisd/test_fuzz_isis_tlv.c +++ b/tests/isisd/test_fuzz_isis_tlv.c @@ -43,7 +43,7 @@ static char *sortlines(char *in) } if (line_count == 1) { - strncpy(rv, in, rv_len); + memcpy(rv, in, rv_len); return rv; } diff --git a/tools/start-stop-daemon.c b/tools/start-stop-daemon.c index 9f566bd1e24f..4406a68f6196 100644 --- a/tools/start-stop-daemon.c +++ b/tools/start-stop-daemon.c @@ -66,6 +66,12 @@ #include #endif +/* this is in zebra.h, but including that here isn't a good fit... */ +#ifndef HAVE_STRLCPY +size_t strlcpy(char *__restrict dest, + const char *__restrict src, size_t destsize); +#endif + static int testmode = 0; static int quietmode = 0; static int exitnodo = 1; @@ -749,8 +755,7 @@ static void do_stop(int signal_nr, int quietmode, int *n_killed, static void set_what_stop(const char *str) { - strncpy(what_stop, str, sizeof(what_stop)); - what_stop[sizeof(what_stop) - 1] = '\0'; + strlcpy(what_stop, str, sizeof(what_stop)); } static int run_stop_schedule(void) diff --git a/tools/subdir.am b/tools/subdir.am index f2ed2332b8bd..d88e4946b481 100644 --- a/tools/subdir.am +++ b/tools/subdir.am @@ -39,7 +39,7 @@ tools_gen_northbound_callbacks_LDADD = lib/libfrr.la $(LIBYANG_LIBS) tools_gen_yang_deviations_SOURCES = tools/gen_yang_deviations.c tools_gen_yang_deviations_LDADD = lib/libfrr.la $(LIBYANG_LIBS) -tools_ssd_SOURCES = tools/start-stop-daemon.c +tools_ssd_SOURCES = tools/start-stop-daemon.c lib/strlcpy.c tools_ssd_CPPFLAGS = # don't bother autoconf'ing these for a simple optional tool