Convert the obvious cases to strlcpy()

This converts the typical idiom of manually terminating the buffer after
a call to strncpy().

Signed-off-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
Phil Sutter 2017-09-01 18:52:52 +02:00 committed by Stephen Hemminger
parent 8d15e012a3
commit 18f156bfec
8 changed files with 8 additions and 16 deletions

View File

@ -518,8 +518,7 @@ int netns_identify_pid(const char *pidstr, char *name, int len)
if ((st.st_dev == netst.st_dev) && if ((st.st_dev == netst.st_dev) &&
(st.st_ino == netst.st_ino)) { (st.st_ino == netst.st_ino)) {
strncpy(name, entry->d_name, len - 1); strlcpy(name, entry->d_name, len);
name[len - 1] = '\0';
} }
} }
closedir(dir); closedir(dir);

View File

@ -325,8 +325,7 @@ static int parse_encap_seg6(struct rtattr *rta, size_t len, int *argcp,
invarg("\"segs\" provided before \"mode\"\n", invarg("\"segs\" provided before \"mode\"\n",
*argv); *argv);
strncpy(segbuf, *argv, 1024); strlcpy(segbuf, *argv, 1024);
segbuf[1023] = 0;
} else if (strcmp(*argv, "hmac") == 0) { } else if (strcmp(*argv, "hmac") == 0) {
NEXT_ARG(); NEXT_ARG();
if (hmac_ok++) if (hmac_ok++)

View File

@ -336,8 +336,7 @@ static int vrf_path(char *vpath, size_t len)
if (vrf) if (vrf)
*vrf = '\0'; *vrf = '\0';
strncpy(vpath, start, len - 1); strlcpy(vpath, start, len);
vpath[len - 1] = '\0';
/* if vrf path is just / then return nothing */ /* if vrf path is just / then return nothing */
if (!strcmp(vpath, "/")) if (!strcmp(vpath, "/"))

View File

@ -512,8 +512,7 @@ static const char *bpf_find_mntpt_single(unsigned long magic, char *mnt,
ret = bpf_valid_mntpt(mntpt, magic); ret = bpf_valid_mntpt(mntpt, magic);
if (!ret) { if (!ret) {
strncpy(mnt, mntpt, len - 1); strlcpy(mnt, mntpt, len);
mnt[len - 1] = 0;
return mnt; return mnt;
} }

View File

@ -172,8 +172,7 @@ int get_command_name(const char *pid, char *comm, size_t len)
if (nl) if (nl)
*nl = '\0'; *nl = '\0';
strncpy(comm, name, len - 1); strlcpy(comm, name, len);
comm[len - 1] = '\0';
break; break;
} }

View File

@ -38,8 +38,7 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
free(ncache); free(ncache);
icache = proto; icache = proto;
ncache = strdup(pe->p_name); ncache = strdup(pe->p_name);
strncpy(buf, pe->p_name, len - 1); strlcpy(buf, pe->p_name, len);
buf[len - 1] = '\0';
return buf; return buf;
} }
snprintf(buf, len, "ipproto-%d", proto); snprintf(buf, len, "ipproto-%d", proto);

View File

@ -425,8 +425,7 @@ static void user_ent_hash_build(void)
user_ent_hash_build_init = 1; user_ent_hash_build_init = 1;
strncpy(name, root, sizeof(name)-1); strlcpy(name, root, sizeof(name));
name[sizeof(name)-1] = 0;
if (strlen(name) == 0 || name[strlen(name)-1] != '/') if (strlen(name) == 0 || name[strlen(name)-1] != '/')
strcat(name, "/"); strcat(name, "/");

View File

@ -145,8 +145,7 @@ get_set_byname(const char *setname, struct xt_set_info *info)
int res; int res;
req.op = IP_SET_OP_GET_BYNAME; req.op = IP_SET_OP_GET_BYNAME;
strncpy(req.set.name, setname, IPSET_MAXNAMELEN); strlcpy(req.set.name, setname, IPSET_MAXNAMELEN);
req.set.name[IPSET_MAXNAMELEN - 1] = '\0';
res = do_getsockopt(&req); res = do_getsockopt(&req);
if (res != 0) if (res != 0)
return -1; return -1;