This fixes gcc-8 warnings about strncpy bounds by using
strlcpy instead.

Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
---
 ip/iplink.c     | 14 +++++++-------
 lib/namespace.c |  6 ++++--
 misc/nstat.c    |  4 ++--
 misc/ss.c       |  2 +-
 tc/m_ematch.c   |  2 +-
 tc/tc_class.c   |  5 +++--
 6 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/ip/iplink.c b/ip/iplink.c
index d401311bcad9..cca530eeeb09 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -1114,7 +1114,7 @@ static int do_chflags(const char *dev, __u32 flags, __u32 
mask)
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
        fd = get_ctl_fd();
        if (fd < 0)
                return -1;
@@ -1141,8 +1141,8 @@ static int do_changename(const char *dev, const char 
*newdev)
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-       strncpy(ifr.ifr_newname, newdev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr.ifr_newname, newdev, IFNAMSIZ);
        fd = get_ctl_fd();
        if (fd < 0)
                return -1;
@@ -1165,7 +1165,7 @@ static int set_qlen(const char *dev, int qlen)
        if (s < 0)
                return -1;
 
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
        if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
                perror("SIOCSIFXQLEN");
                close(s);
@@ -1185,7 +1185,7 @@ static int set_mtu(const char *dev, int mtu)
        if (s < 0)
                return -1;
 
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
        if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
                perror("SIOCSIFMTU");
                close(s);
@@ -1212,7 +1212,7 @@ static int get_address(const char *dev, int *htype)
                return -1;
        }
 
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
        if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
                perror("SIOCGIFINDEX");
                close(s);
@@ -1243,7 +1243,7 @@ static int parse_address(const char *dev, int hatype, int 
halen,
        int alen;
 
        memset(ifr, 0, sizeof(*ifr));
-       strncpy(ifr->ifr_name, dev, IFNAMSIZ);
+       strlcpy(ifr->ifr_name, dev, IFNAMSIZ);
        ifr->ifr_hwaddr.sa_family = hatype;
        alen = ll_addr_a2n(ifr->ifr_hwaddr.sa_data, 14, lla);
        if (alen < 0)
diff --git a/lib/namespace.c b/lib/namespace.c
index 6f3356d0fa08..682634028587 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -23,7 +23,8 @@ static void bind_etc(const char *name)
        struct dirent *entry;
        DIR *dir;
 
-       snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", 
NETNS_ETC_DIR, name);
+       snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s",
+                NETNS_ETC_DIR, name);
        dir = opendir(etc_netns_path);
        if (!dir)
                return;
@@ -33,7 +34,8 @@ static void bind_etc(const char *name)
                        continue;
                if (strcmp(entry->d_name, "..") == 0)
                        continue;
-               snprintf(netns_name, sizeof(netns_name), "%s/%s", 
etc_netns_path, entry->d_name);
+               snprintf(netns_name, sizeof(netns_name),
+                        "%s/%s", etc_netns_path, entry->d_name);
                snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
                if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
                        fprintf(stderr, "Bind %s -> %s failed: %s\n",
diff --git a/misc/nstat.c b/misc/nstat.c
index a4dd405d43a9..433a1f483be3 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -178,12 +178,12 @@ static int count_spaces(const char *line)
 
 static void load_ugly_table(FILE *fp)
 {
-       char buf[4096];
+       char buf[2048];
        struct nstat_ent *db = NULL;
        struct nstat_ent *n;
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
-               char idbuf[sizeof(buf)];
+               char idbuf[4096];
                int  off;
                char *p;
                int count1, count2, skip = 0;
diff --git a/misc/ss.c b/misc/ss.c
index e087bef739b0..a03fa4a7c174 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -4032,7 +4032,7 @@ static int netlink_show_one(struct filter *f,
 
                if (!pid) {
                        done = 1;
-                       strncpy(procname, "kernel", 6);
+                       strncpy(procname, "kernel", 7);
                } else if (pid > 0) {
                        FILE *fp;
 
diff --git a/tc/m_ematch.c b/tc/m_ematch.c
index d2bb5c380382..ee8981f0146c 100644
--- a/tc/m_ematch.c
+++ b/tc/m_ematch.c
@@ -161,7 +161,7 @@ static struct ematch_util *get_ematch_kind(char *kind)
 
 static struct ematch_util *get_ematch_kind_num(__u16 kind)
 {
-       char name[32];
+       char name[512];
 
        if (lookup_map(kind, name, sizeof(name), EMATCH_MAP) < 0)
                return NULL;
diff --git a/tc/tc_class.c b/tc/tc_class.c
index 1b214b82c702..91802518bb27 100644
--- a/tc/tc_class.c
+++ b/tc/tc_class.c
@@ -219,7 +219,7 @@ static void graph_cls_show(FILE *fp, char *buf, struct 
hlist_head *root_list,
        char cls_id_str[256] = {};
        struct rtattr *tb[TCA_MAX + 1];
        struct qdisc_util *q;
-       char str[100] = {};
+       char str[300] = {};
 
        hlist_for_each_safe(n, tmp_cls, root_list) {
                struct hlist_node *c, *tmp_chld;
@@ -242,7 +242,8 @@ static void graph_cls_show(FILE *fp, char *buf, struct 
hlist_head *root_list,
                graph_indent(buf, cls, 0, 0);
 
                print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id);
-               sprintf(str, "+---(%s)", cls_id_str);
+               snprintf(str, sizeof(str),
+                        "+---(%s)", cls_id_str);
                strcat(buf, str);
 
                parse_rtattr(tb, TCA_MAX, (struct rtattr *)cls->data,
-- 
2.16.2

Reply via email to