]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
Convert the obvious cases to strlcpy()
authorPhil Sutter <phil@nwl.cc>
Fri, 1 Sep 2017 16:52:52 +0000 (18:52 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 1 Sep 2017 19:10:54 +0000 (12:10 -0700)
This converts the typical idiom of manually terminating the buffer after
a call to strncpy().

Signed-off-by: Phil Sutter <phil@nwl.cc>
ip/ipnetns.c
ip/iproute_lwtunnel.c
ip/ipvrf.c
lib/bpf.c
lib/fs.c
lib/inet_proto.c
misc/ss.c
tc/em_ipset.c

index 9ee1fe6a51f9c52f6bae7f287f7273643f321148..afb4978a5be7da0caa5f5993d8f8d5ff0d960208 100644 (file)
@@ -518,8 +518,7 @@ int netns_identify_pid(const char *pidstr, char *name, int len)
 
                if ((st.st_dev == netst.st_dev) &&
                    (st.st_ino == netst.st_ino)) {
-                       strncpy(name, entry->d_name, len - 1);
-                       name[len - 1] = '\0';
+                       strlcpy(name, entry->d_name, len);
                }
        }
        closedir(dir);
index 1a3dc4d4c0ed96003afccb91d56986b02cb2c601..4c2d3b07e3b5cd8956904f7807824c7d6d13069d 100644 (file)
@@ -325,8 +325,7 @@ static int parse_encap_seg6(struct rtattr *rta, size_t len, int *argcp,
                                invarg("\"segs\" provided before \"mode\"\n",
                                       *argv);
 
-                       strncpy(segbuf, *argv, 1024);
-                       segbuf[1023] = 0;
+                       strlcpy(segbuf, *argv, 1024);
                } else if (strcmp(*argv, "hmac") == 0) {
                        NEXT_ARG();
                        if (hmac_ok++)
index e6fad32abd956c6159efce21ee06679d5ad3948f..b74c501e0970c798053a3f50b79cc3352759c495 100644 (file)
@@ -336,8 +336,7 @@ static int vrf_path(char *vpath, size_t len)
                if (vrf)
                        *vrf = '\0';
 
-               strncpy(vpath, start, len - 1);
-               vpath[len - 1] = '\0';
+               strlcpy(vpath, start, len);
 
                /* if vrf path is just / then return nothing */
                if (!strcmp(vpath, "/"))
index 0bd0a95eafe6cbc6309784c61a796f82c3869be7..c180934acc7dc3d422c88e777bbc448df69ab8d6 100644 (file)
--- a/lib/bpf.c
+++ b/lib/bpf.c
@@ -512,8 +512,7 @@ static const char *bpf_find_mntpt_single(unsigned long magic, char *mnt,
 
        ret = bpf_valid_mntpt(mntpt, magic);
        if (!ret) {
-               strncpy(mnt, mntpt, len - 1);
-               mnt[len - 1] = 0;
+               strlcpy(mnt, mntpt, len);
                return mnt;
        }
 
index ebe05cd44e11bca1ff1481a3c628db61fd2a1fae..86efd4ed2ed8084ad147b5d53e62ac2458ccf289 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -172,8 +172,7 @@ int get_command_name(const char *pid, char *comm, size_t len)
                if (nl)
                        *nl = '\0';
 
-               strncpy(comm, name, len - 1);
-               comm[len - 1] = '\0';
+               strlcpy(comm, name, len);
                break;
        }
 
index 53c029039b6d5f56a9506db2f690e920bb5e97c9..bdfd52fdafe5a828f9543b8cdfcbf17cc170c329 100644 (file)
@@ -38,8 +38,7 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
                        free(ncache);
                icache = proto;
                ncache = strdup(pe->p_name);
-               strncpy(buf, pe->p_name, len - 1);
-               buf[len - 1] = '\0';
+               strlcpy(buf, pe->p_name, len);
                return buf;
        }
        snprintf(buf, len, "ipproto-%d", proto);
index 2c9e80e696595061e3cee41fcabf7d464ed942e3..dd8dfaa4e70db02a92eb6cf855e3c2957ad0d938 100644 (file)
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -425,8 +425,7 @@ static void user_ent_hash_build(void)
 
        user_ent_hash_build_init = 1;
 
-       strncpy(name, root, sizeof(name)-1);
-       name[sizeof(name)-1] = 0;
+       strlcpy(name, root, sizeof(name));
 
        if (strlen(name) == 0 || name[strlen(name)-1] != '/')
                strcat(name, "/");
index b59756515d2394d178ccead5d0dd6d4d70d58332..48b287f5ba3b267259f864d21033672e1287d968 100644 (file)
@@ -145,8 +145,7 @@ get_set_byname(const char *setname, struct xt_set_info *info)
        int res;
 
        req.op = IP_SET_OP_GET_BYNAME;
-       strncpy(req.set.name, setname, IPSET_MAXNAMELEN);
-       req.set.name[IPSET_MAXNAMELEN - 1] = '\0';
+       strlcpy(req.set.name, setname, IPSET_MAXNAMELEN);
        res = do_getsockopt(&req);
        if (res != 0)
                return -1;