Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: remove remaining strncpy() users #17156

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 2 additions & 4 deletions lib/ptm_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Copyright (C) 2015 Cumulus Networks, Inc.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <zebra.h>

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions tests/helpers/c/prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/isisd/test_fuzz_isis_tlv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 7 additions & 2 deletions tools/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
#include <linux/sched.h>
#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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tools/subdir.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading