Whamcloud - gitweb
LU-9934 build: address issues raised by gcc7
[fs/lustre-release.git] / lustre / utils / nidlist.c
index 78e70ce..87450a4 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/utils/nidlist.c
  *
@@ -53,8 +50,6 @@ struct nl_struct {
 };
 #define NL_CHUNK       64
 
-extern char *prog;
-
 static void nl_oom(void)
 {
        fprintf(stderr, "%s: out of memory\n", prog);
@@ -65,10 +60,12 @@ NIDList nl_create(void)
 {
        struct nl_struct *nl;
 
-       if (!(nl = malloc(sizeof(struct nl_struct))))
+       nl = malloc(sizeof(struct nl_struct));
+       if (!nl)
                nl_oom();
        nl->len = NL_CHUNK;
-       if (!(nl->nids = malloc(nl->len * sizeof(char *))))
+       nl->nids = malloc(nl->len * sizeof(char *));
+       if (!nl->nids)
                nl_oom();
        nl->count = 0;
 
@@ -88,7 +85,8 @@ void nl_destroy(NIDList nl)
 static void nl_grow(NIDList nl, int n)
 {
        nl->len += n;
-       if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
+       nl->nids = realloc(nl->nids, nl->len * sizeof(char *));
+       if (!nl->nids)
                nl_oom();
 }
 
@@ -96,7 +94,8 @@ void nl_add(NIDList nl, char *nid)
 {
        char *cp;
 
-       if (!(cp = strdup(nid)))
+       cp = strdup(nid);
+       if (!cp)
                nl_oom();
        if (nl->count == nl->len)
                nl_grow(nl, NL_CHUNK);
@@ -105,16 +104,18 @@ void nl_add(NIDList nl, char *nid)
 
 int nl_count(NIDList nl)
 {
-        return nl->count;
+       return nl->count;
 }
 
 static char *nl_nid_addr(char *nid)
 {
        char *addr, *p;
 
-       if (!(addr = strdup(nid)))
+       addr = strdup(nid);
+       if (!addr)
                nl_oom();
-       if ((p = strchr(addr, '@')))
+       p = strchr(addr, '@');
+       if (p)
                *p = '\0';
 
        return addr;
@@ -125,7 +126,7 @@ static int nl_nid_parse_addr(char *addr)
        int o;
 
        for (o = strlen(addr); o > 0; o--)
-                if (!isdigit(addr[o - 1]))
+               if (!isdigit(addr[o - 1]))
                        break;
 
        return o;
@@ -140,12 +141,13 @@ static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
        o1 = nl_nid_parse_addr(p1);
        o2 = nl_nid_parse_addr(p2);
 
-       if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 0) {
+       res = strncmp(p1, p2, o1);
+       if (o1 == o2 && res == 0) {
                res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
-                if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
-                        cflag = 1;
+               if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
+                       cflag = 1;
        } else
-                res = strcmp(p1, p2);
+               res = strcmp(p1, p2);
        free(p1);
        free(p2);
        if (cflagp)
@@ -165,7 +167,8 @@ static int nl_cmp(const void *p1, const void *p2)
 {
        int res;
 
-       if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
+       res = nl_cmp_lnet(*(char **)p1, *(char **)p2);
+       if (res == 0)
                res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
        return res;
 }
@@ -192,50 +195,58 @@ void nl_uniq(NIDList nl)
 
 static char *nl_nid_lookup_ipaddr(char *nid)
 {
-        struct addrinfo *ai, *aip;
-        char name[NI_MAXHOST] = "";
-        char *p, *addr, *lnet = NULL, *res = NULL;
-        int len, x;
-
-        addr = nl_nid_addr(nid);
-        if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
-                if ((p = strchr(nid, '@')))
-                        lnet = p + 1;
-                if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
-                        for (aip = ai; aip != NULL; aip = aip->ai_next) {
-                                if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
-                                    name, sizeof(name), NULL, 0,
-                                    NI_NAMEREQD | NI_NOFQDN) == 0) {
-                                        if ((p = strchr(name, '.')))
-                                                *p = '\0';
+       struct addrinfo *ai, *aip;
+       char name[NI_MAXHOST] = "";
+       char *p, *addr, *lnet = NULL, *res = NULL;
+       int len, x;
+
+       addr = nl_nid_addr(nid);
+       if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
+               p = strchr(nid, '@');
+               if (p)
+                       lnet = p + 1;
+               if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
+                       for (aip = ai; aip != NULL; aip = aip->ai_next) {
+                               if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
+                                   name, sizeof(name), NULL, 0,
+                                   NI_NAMEREQD | NI_NOFQDN) == 0) {
+                                       p = strchr(name, '.');
+                                       if (p)
+                                               *p = '\0';
                                        len = strlen(name) + 2;
-                                       if (lnet)
+                                       if (lnet != NULL)
                                                len += strlen(lnet);
-                                        if (!(res = malloc(len)))
-                                                nl_oom();
-                                        snprintf(res, len, "%s@%s", name, lnet);
-                                        break;
-                                }
-                        }
-                        freeaddrinfo(ai);
-                }
-        }
-        free(addr);
-
-        return res;
+                                       res = malloc(len);
+                                       if (!res)
+                                               nl_oom();
+                                       if (lnet != NULL)
+                                               snprintf(res, len, "%s@%s",
+                                                        name, lnet);
+                                       else
+                                               snprintf(res, len, "%s", name);
+                                       break;
+                               }
+                       }
+                       freeaddrinfo(ai);
+               }
+       }
+       free(addr);
+
+       return res;
 }
 
 void nl_lookup_ip(NIDList nl)
 {
        int i;
-        char *new;
+       char *new;
 
        for (i = 0; i < nl->count; i++) {
-                if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
-                        free(nl->nids[i]);
-                        nl->nids[i] = new;
-                }
-        }
+               new = nl_nid_lookup_ipaddr(nl->nids[i]);
+               if (new) {
+                       free(nl->nids[i]);
+                       nl->nids[i] = new;
+               }
+       }
 }
 
 char *nl_string(NIDList nl, char *sep)
@@ -246,56 +257,65 @@ char *nl_string(NIDList nl, char *sep)
 
        for (i = 0; i < nl->count; i++)
                len += strlen(nl->nids[i]) + seplen;
-       if (!(s = malloc(len)))
+       s = malloc(len);
+       if (!s)
                nl_oom();
        s[0] = '\0';
        for (i = 0; i < nl->count; i++) {
                if (i > 0)
-                       strcat(s, sep);
-               strcat(s, nl->nids[i]);
+                       strncat(s, sep, len);
+               strncat(s, nl->nids[i], len);
        }
        return s;
 }
 
-static void nl_strxcat(char *s, char **nids, int len)
+static void nl_strxcat(char *s, char **nids, int len, const int max_len)
 {
        int i, o, lastn = 0;
        char *base, *p, *lnet = NULL, *savedn = NULL;
 
-       if ((p = strchr(nids[0], '@')))
+       p = strchr(nids[0], '@');
+       if (p)
                lnet = p + 1;
-        base = nl_nid_addr(nids[0]);
-        o = nl_nid_parse_addr(base);
+       base = nl_nid_addr(nids[0]);
+       o = nl_nid_parse_addr(base);
        base[o] = '\0';
        for (i = 0; i < len; i++) {
                char *addr = nl_nid_addr(nids[i]);
                int n = strtoul(&addr[o], NULL, 10);
 
                if (i == 0)
-                       sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
+                       snprintf(s + strlen(s), max_len, "%s[%s", base,
+                                &addr[o]);
                else if (i < len) {
                        if (n == lastn + 1) {
                                if (savedn)
                                        free(savedn);
-                               if (!(savedn = strdup(&addr[o])))
+                               savedn = strdup(&addr[o]);
+                               if (!savedn)
                                        nl_oom();
                        } else {
                                if (savedn) {
-                                       sprintf(s + strlen(s), "-%s", savedn);
+                                       snprintf(s + strlen(s),
+                                                max_len - strlen(s),
+                                                "-%s", savedn);
                                        free(savedn);
                                        savedn = NULL;
                                }
-                               sprintf(s + strlen(s), ",%s", &addr[o]);
+                               snprintf(s + strlen(s), max_len - strlen(s),
+                                        ",%s", &addr[o]);
                        }
                }
                if (i == len - 1) {
                        if (savedn) {
-                               sprintf(s + strlen(s), "-%s", savedn);
+                               snprintf(s + strlen(s), max_len - strlen(s),
+                                        "-%s", savedn);
                                free(savedn);
                        }
-                       strcat(s, "]");
+                       strncat(s, "]", 1);
                        if (lnet)
-                               sprintf(s + strlen(s), "@%s", lnet);
+                               snprintf(s + strlen(s), max_len - strlen(s),
+                                        "@%s", lnet);
                }
                free(addr);
                lastn = n;
@@ -311,12 +331,13 @@ char *nl_xstring(NIDList nl, char *sep)
 
        for (i = 0; i < nl->count; i++)
                len += strlen(nl->nids[i]) + seplen;
-       if (!(s = malloc(len)))
+       s = malloc(len);
+       if (!s)
                nl_oom();
        s[0] = '\0';
        for (i = 0; i < nl->count; i++) {
                if (i > 0)
-                       strcat(s, sep);
+                       strncat(s, sep, len);
                for (j = i + 1; j < nl->count; j++) {
                        if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
                                break;
@@ -325,9 +346,9 @@ char *nl_xstring(NIDList nl, char *sep)
                                break;
                }
                if (j - i > 1)
-                       nl_strxcat(s, &nl->nids[i], j - i);
+                       nl_strxcat(s, &nl->nids[i], j - i, len);
                else
-                       strcat(s, nl->nids[i]);
+                       strncat(s, nl->nids[i], len);
                i += j - i - 1;
        }
        return s;