]> The Tcpdump Group git mirrors - tcpdump/blobdiff - addrtoname.c
Remove more old-compiler compensation.
[tcpdump] / addrtoname.c
index b6b49aa48c80ca9ab8d5bad05e73e977dfa9d5c8..aa43be768c1efa8270842707405ac8ae701e812e 100644 (file)
@@ -184,7 +184,7 @@ win32_gethostbyaddr(const char *addr, int len, int type)
 #endif /* _WIN32 */
 
 struct h6namemem {
-       struct in6_addr addr;
+       nd_ipv6 addr;
        char *name;
        struct h6namemem *nxt;
 };
@@ -242,13 +242,13 @@ intoa(uint32_t addr)
        n = 4;
        do {
                byte = addr & 0xff;
-               *--cp = byte % 10 + '0';
+               *--cp = (char)(byte % 10) + '0';
                byte /= 10;
                if (byte > 0) {
-                       *--cp = byte % 10 + '0';
+                       *--cp = (char)(byte % 10) + '0';
                        byte /= 10;
                        if (byte > 0)
-                               *--cp = byte + '0';
+                               *--cp = (char)byte + '0';
                }
                *--cp = '.';
                addr >>= 8;
@@ -269,7 +269,7 @@ extern cap_channel_t *capdns;
  *
  * NOTE: ap is *NOT* necessarily part of the packet data (not even if
  * this is being called with the "ipaddr_string()" macro), so you
- * *CANNOT* use the ND_TCHECK{2}/ND_TTEST{2} macros on it.  Furthermore,
+ * *CANNOT* use the ND_TCHECK_* or ND_TTEST_* macros on it.  Furthermore,
  * even in cases where it *is* part of the packet data, the caller
  * would still have to check for a null return value, even if it's
  * just printing the return value with "%s" - not all versions of
@@ -346,7 +346,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
 {
        struct hostent *hp;
        union {
-               struct in6_addr addr;
+               nd_ipv6 addr;
                struct for_hash_addr {
                        char fill[14];
                        uint16_t d;
@@ -362,7 +362,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
                if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
                        return (p->name);
        }
-       p->addr = addr.addr;
+       memcpy(p->addr, addr.addr, sizeof(nd_ipv6));
        p->nxt = newh6namemem(ndo);
 
        /*
@@ -406,6 +406,38 @@ static const char hex[16] = {
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 };
 
+/*
+ * Convert an octet to two hex digits.
+ *
+ * Coverity appears either:
+ *
+ *    not to believe the C standard when it asserts that a uint8_t is
+ *    exactly 8 bits in size;
+ *
+ *    not to believe that an unsigned type of exactly 8 bits has a value
+ *    in the range of 0 to 255;
+ *
+ *    not to believe that, for a range of unsigned values, if you shift
+ *    one of those values right by 4 bits, the maximum result value is
+ *    the maximum value shifted right by 4 bits, with no stray 1's shifted
+ *    in;
+ *
+ *    not to believe that 255 >> 4 is 15;
+ *
+ * so it gets upset that we're taking a "tainted" unsigned value, shifting
+ * it right 4 bits, and using it as an index into a 16-element array.
+ *
+ * So we do a stupid pointless masking of the result of the shift with
+ * 0xf, to hammer the point home to Coverity.
+ */
+static inline char *
+octet_to_hex(char *cp, uint8_t octet)
+{
+       *cp++ = hex[(octet >> 4) & 0xf];
+       *cp++ = hex[(octet >> 0) & 0xf];
+       return (cp);
+}
+
 /* Find the hash node that corresponds the ether address 'ep' */
 
 static struct enamemem *
@@ -426,9 +458,9 @@ lookup_emem(netdissect_options *ndo, const u_char *ep)
                        return tp;
                else
                        tp = tp->e_nxt;
-       tp->e_addr0 = i;
-       tp->e_addr1 = j;
-       tp->e_addr2 = k;
+       tp->e_addr0 = (u_short)i;
+       tp->e_addr1 = (u_short)j;
+       tp->e_addr2 = (u_short)k;
        tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
        if (tp->e_nxt == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "lookup_emem: calloc");
@@ -470,9 +502,9 @@ lookup_bytestring(netdissect_options *ndo, const u_char *bs,
                else
                        tp = tp->bs_nxt;
 
-       tp->bs_addr0 = i;
-       tp->bs_addr1 = j;
-       tp->bs_addr2 = k;
+       tp->bs_addr0 = (u_short)i;
+       tp->bs_addr1 = (u_short)j;
+       tp->bs_addr2 = (u_short)k;
 
        tp->bs_bytes = (u_char *) calloc(1, nlen);
        if (tp->bs_bytes == NULL)
@@ -519,9 +551,9 @@ lookup_nsap(netdissect_options *ndo, const u_char *nsap,
                        return tp;
                else
                        tp = tp->e_nxt;
-       tp->e_addr0 = i;
-       tp->e_addr1 = j;
-       tp->e_addr2 = k;
+       tp->e_addr0 = (u_short)i;
+       tp->e_addr1 = (u_short)j;
+       tp->e_addr2 = (u_short)k;
        tp->e_nsap = (u_char *)malloc(nsap_length + 1);
        if (tp->e_nsap == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "lookup_nsap: malloc");
@@ -554,7 +586,7 @@ lookup_protoid(netdissect_options *ndo, const u_char *pi)
                else
                        tp = tp->p_nxt;
        tp->p_oui = i;
-       tp->p_proto = j;
+       tp->p_proto = (u_short)j;
        tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
        if (tp->p_nxt == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "lookup_protoid: calloc");
@@ -589,16 +621,14 @@ etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
 #endif
        cp = buf;
        oui = EXTRACT_BE_U_3(ep);
-       *cp++ = hex[*ep >> 4 ];
-       *cp++ = hex[*ep++ & 0xf];
+       cp = octet_to_hex(cp, *ep++);
        for (i = 5; --i >= 0;) {
                *cp++ = ':';
-               *cp++ = hex[*ep >> 4 ];
-               *cp++ = hex[*ep++ & 0xf];
+               cp = octet_to_hex(cp, *ep++);
        }
 
        if (!ndo->ndo_nflag) {
-               nd_snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
+               snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
                    tok2str(oui_values, "Unknown", oui));
        } else
                *cp = '\0';
@@ -624,8 +654,7 @@ le64addr_string(netdissect_options *ndo, const uint8_t *ep)
 
        cp = buf;
        for (i = len; i > 0 ; --i) {
-               *cp++ = hex[*(ep + i - 1) >> 4];
-               *cp++ = hex[*(ep + i - 1) & 0xf];
+               cp = octet_to_hex(cp, *(ep + i - 1));
                *cp++ = ':';
        }
        cp --;
@@ -665,12 +694,10 @@ linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
        if (tp->bs_name == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
                                  "linkaddr_string: malloc");
-       *cp++ = hex[*ep >> 4];
-       *cp++ = hex[*ep++ & 0xf];
+       cp = octet_to_hex(cp, *ep++);
        for (i = len-1; i > 0 ; --i) {
                *cp++ = ':';
-               *cp++ = hex[*ep >> 4];
-               *cp++ = hex[*ep++ & 0xf];
+               cp = octet_to_hex(cp, *ep++);
        }
        *cp = '\0';
        return (tp->bs_name);
@@ -698,8 +725,7 @@ isonsap_string(netdissect_options *ndo, const uint8_t *nsap,
                                  "isonsap_string: malloc");
 
        for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
-               *cp++ = hex[*nsap >> 4];
-               *cp++ = hex[*nsap++ & 0xf];
+               cp = octet_to_hex(cp, *nsap++);
                if (((nsap_idx & 1) == 0) &&
                     (nsap_idx + 1 < nsap_length)) {
                        *cp++ = '.';
@@ -723,7 +749,7 @@ tcpport_string(netdissect_options *ndo, u_short port)
        tp->addr = i;
        tp->nxt = newhnamemem(ndo);
 
-       (void)nd_snprintf(buf, sizeof(buf), "%u", i);
+       (void)snprintf(buf, sizeof(buf), "%u", i);
        tp->name = strdup(buf);
        if (tp->name == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
@@ -745,7 +771,7 @@ udpport_string(netdissect_options *ndo, u_short port)
        tp->addr = i;
        tp->nxt = newhnamemem(ndo);
 
-       (void)nd_snprintf(buf, sizeof(buf), "%u", i);
+       (void)snprintf(buf, sizeof(buf), "%u", i);
        tp->name = strdup(buf);
        if (tp->name == NULL)
                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
@@ -803,7 +829,7 @@ init_servarray(netdissect_options *ndo)
                while (table->name)
                        table = table->nxt;
                if (ndo->ndo_nflag) {
-                       (void)nd_snprintf(buf, sizeof(buf), "%d", port);
+                       (void)snprintf(buf, sizeof(buf), "%d", port);
                        table->name = strdup(buf);
                } else
                        table->name = strdup(sv->s_name);
@@ -979,7 +1005,10 @@ init_etherarray(netdissect_options *ndo)
        }
 }
 
-static const struct tok ipxsap_db[] = {
+static const struct ipxsap_ent {
+       uint16_t        v;
+       const char      *s;
+} ipxsap_db[] = {
        { 0x0000, "Unknown" },
        { 0x0001, "User" },
        { 0x0002, "User Group" },
@@ -1203,7 +1232,7 @@ init_ipxsaparray(netdissect_options *ndo)
        struct hnamemem *table;
 
        for (i = 0; ipxsap_db[i].s != NULL; i++) {
-               int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
+               u_int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
                table = &ipxsaptable[j];
                while (table->name)
                        table = table->nxt;
@@ -1304,7 +1333,7 @@ const char *
 ieee8021q_tci_string(const uint16_t tci)
 {
        static char buf[128];
-       nd_snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
+       snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
                 tci & 0xfff,
                 tci >> 13,
                 (tci & 0x1000) ? ", DEI" : "");