From: Renato Westphal Date: Fri, 25 Aug 2017 00:43:29 +0000 (-0300) Subject: *: fix assorted issues detected by Coverity Scan X-Git-Tag: frr-4.0-dev~355^2~2 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=refs%2Fpull%2F1042%2Fhead;p=mirror%2Ffrr.git *: fix assorted issues detected by Coverity Scan Signed-off-by: Renato Westphal --- diff --git a/bgpd/bgp_evpn.c b/bgpd/bgp_evpn.c index 0560dc46f9..75c1237bca 100644 --- a/bgpd/bgp_evpn.c +++ b/bgpd/bgp_evpn.c @@ -2042,7 +2042,7 @@ static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p, /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */ stream_putc(s, 8 + 10 + 4 + 1 + len + 3); stream_put(s, prd->val, 8); - if (attr && attr) + if (attr) stream_put(s, &(attr->evpn_overlay.eth_s_id), 10); else stream_put(s, &temp, 10); @@ -2052,7 +2052,7 @@ static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p, stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr); else stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16); - if (attr && attr) { + if (attr) { if (IS_IPADDR_V4(&p_evpn_p->ip)) stream_put_ipv4(s, attr->evpn_overlay.gw_ip.ipv4.s_addr); diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index beb1e2a2a0..768b2b8fb6 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -10298,10 +10298,6 @@ static int bgp_show_neighbor_route(struct vty *vty, struct peer *peer, return CMD_WARNING; } - /* labeled-unicast routes live in the unicast table */ - if (safi == SAFI_LABELED_UNICAST) - safi = SAFI_UNICAST; - return bgp_show(vty, peer->bgp, afi, safi, type, &peer->su, use_json); } diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 5a5d2a5d5d..307880f9d1 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -658,8 +658,10 @@ static void *route_match_vni_compile(const char *arg) return NULL; *vni = strtoul(arg, &end, 10); - if (*end != '\0') + if (*end != '\0') { + XFREE(MTYPE_ROUTE_MAP_COMPILED, vni); return NULL; + } return vni; } diff --git a/bgpd/rfapi/bgp_rfapi_cfg.c b/bgpd/rfapi/bgp_rfapi_cfg.c index c8e2dd9525..e3b446f76a 100644 --- a/bgpd/rfapi/bgp_rfapi_cfg.c +++ b/bgpd/rfapi/bgp_rfapi_cfg.c @@ -2977,11 +2977,6 @@ DEFUN_NOSH (vnc_vrf_policy, struct rfapi_nve_group_cfg *rfg; VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* Search for name */ rfg = bgp_rfapi_cfg_match_byname(bgp, argv[1]->arg, RFAPI_GROUP_CFG_VRF); @@ -3012,10 +3007,6 @@ DEFUN (vnc_no_vrf_policy, { VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } return bgp_rfapi_delete_named_nve_group(vty, bgp, argv[2]->arg, RFAPI_GROUP_CFG_VRF); } @@ -3031,11 +3022,6 @@ DEFUN (vnc_vrf_policy_label, uint32_t label; VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->nve_groups_sequential, rfg)) { /* Not in list anymore */ @@ -3145,11 +3131,6 @@ DEFUN (vnc_vrf_policy_rt_import, int is_export_bgp = 0; int is_export_zebra = 0; - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->nve_groups_sequential, rfg)) { /* Not in list anymore */ @@ -3213,11 +3194,6 @@ DEFUN (vnc_vrf_policy_rt_export, VTY_DECLVAR_CONTEXT(bgp, bgp); int rc; - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->nve_groups_sequential, rfg)) { /* Not in list anymore */ @@ -3253,11 +3229,6 @@ DEFUN (vnc_vrf_policy_rt_both, struct listnode *node; struct rfapi_rfg_name *rfgn; - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->nve_groups_sequential, rfg)) { /* Not in list anymore */ @@ -3333,11 +3304,6 @@ DEFUN (vnc_vrf_policy_rd, VTY_DECLVAR_CONTEXT_SUB(rfapi_nve_group_cfg, rfg); VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->nve_groups_sequential, rfg)) { /* Not in list anymore */ @@ -3420,11 +3386,6 @@ DEFUN_NOSH (vnc_l2_group, struct rfapi_l2_group_cfg *rfg; VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* Search for name */ rfg = rfapi_l2_group_lookup_byname(bgp, argv[1]->arg); @@ -3505,10 +3466,6 @@ DEFUN (vnc_no_l2_group, { VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } return bgp_rfapi_delete_named_l2_group(vty, bgp, argv[3]->arg); } @@ -3522,11 +3479,6 @@ DEFUN (vnc_l2_group_lni, VTY_DECLVAR_CONTEXT_SUB(rfapi_l2_group_cfg, rfg); VTY_DECLVAR_CONTEXT(bgp, bgp); - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->l2_groups, rfg)) { /* Not in list anymore */ @@ -3549,11 +3501,6 @@ DEFUN (vnc_l2_group_labels, VTY_DECLVAR_CONTEXT(bgp, bgp); struct list *ll; - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->l2_groups, rfg)) { /* Not in list anymore */ @@ -3589,11 +3536,6 @@ DEFUN (vnc_l2_group_no_labels, VTY_DECLVAR_CONTEXT(bgp, bgp); struct list *ll; - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } - /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->l2_groups, rfg)) { /* Not in list anymore */ @@ -3646,10 +3588,6 @@ DEFUN (vnc_l2_group_rt, vty_out(vty, "Unknown option, %s\n", argv[1]->arg); return CMD_ERR_NO_MATCH; } - if (!bgp) { - vty_out(vty, "No BGP process is configured\n"); - return CMD_WARNING_CONFIG_FAILED; - } /* make sure it's still in list */ if (!listnode_lookup(bgp->rfapi_cfg->l2_groups, rfg)) { diff --git a/bgpd/rfapi/rfapi_import.c b/bgpd/rfapi/rfapi_import.c index d63975a22b..3f427c3903 100644 --- a/bgpd/rfapi/rfapi_import.c +++ b/bgpd/rfapi/rfapi_import.c @@ -3875,10 +3875,9 @@ rfapiBgpInfoFilteredImportFunction(safi_t safi) default: /* not expected */ + zlog_err("%s: bad safi %d", __func__, safi); return NULL; } - zlog_err("%s: bad safi %d", __func__, safi); - return NULL; } void rfapiProcessUpdate(struct peer *peer, diff --git a/lib/routemap.c b/lib/routemap.c index a70248633c..5f74189700 100644 --- a/lib/routemap.c +++ b/lib/routemap.c @@ -2201,7 +2201,7 @@ DEFUN (no_set_ip_nexthop, "Next hop address\n" "IP address of next hop\n") { - int idx; + int idx = 0; VTY_DECLVAR_CONTEXT(route_map_index, index); const char *arg = NULL; diff --git a/ospf6d/ospf6_zebra.c b/ospf6d/ospf6_zebra.c index 3443bc47b6..2d04790d23 100644 --- a/ospf6d/ospf6_zebra.c +++ b/ospf6d/ospf6_zebra.c @@ -231,11 +231,7 @@ static int ospf6_zebra_read_route(int command, struct zclient *zclient, char prefixstr[PREFIX2STR_BUFFER], nexthopstr[128]; prefix2str((struct prefix *)&api.prefix, prefixstr, sizeof(prefixstr)); - if (nexthop) - inet_ntop(AF_INET6, nexthop, nexthopstr, - sizeof(nexthopstr)); - else - snprintf(nexthopstr, sizeof(nexthopstr), "::"); + inet_ntop(AF_INET6, nexthop, nexthopstr, sizeof(nexthopstr)); zlog_debug( "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI, diff --git a/ospfd/ospf_te.c b/ospfd/ospf_te.c index 1c1c76c1af..5517008909 100644 --- a/ospfd/ospf_te.c +++ b/ospfd/ospf_te.c @@ -2425,7 +2425,7 @@ DEFUN (show_ip_ospf_mpls_te_router, if (ntohs(OspfMplsTE.router_addr.header.type) != 0) show_vty_router_addr(vty, &OspfMplsTE.router_addr.header); - else if (vty != NULL) + else vty_out(vty, " N/A\n"); } return CMD_SUCCESS; diff --git a/ripd/ripd.c b/ripd/ripd.c index d9b38bba89..36eceafd9b 100644 --- a/ripd/ripd.c +++ b/ripd/ripd.c @@ -1685,6 +1685,7 @@ int rip_recvmsg(int sock, u_char *buf, int size, struct sockaddr_in *from, struct cmsghdr *ptr; char adata[1024]; + memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)from; msg.msg_namelen = sizeof(struct sockaddr_in); msg.msg_iov = &iov; diff --git a/ripngd/ripngd.c b/ripngd/ripngd.c index 2883698f47..d8017622d1 100644 --- a/ripngd/ripngd.c +++ b/ripngd/ripngd.c @@ -176,6 +176,7 @@ int ripng_send_packet(caddr_t buf, int bufsize, struct sockaddr_in6 *to, addr.sin6_port = htons(RIPNG_PORT_DEFAULT); } + memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)&addr; msg.msg_namelen = sizeof(struct sockaddr_in6); msg.msg_iov = &iov; @@ -228,6 +229,7 @@ static int ripng_recv_packet(int sock, u_char *buf, int bufsize, char adata[1024]; /* Fill in message and iovec. */ + memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)from; msg.msg_namelen = sizeof(struct sockaddr_in6); msg.msg_iov = &iov; diff --git a/zebra/if_netlink.c b/zebra/if_netlink.c index 0d08155178..ff69ce47cf 100644 --- a/zebra/if_netlink.c +++ b/zebra/if_netlink.c @@ -393,7 +393,7 @@ static int get_iflink_speed(const char *ifname) memset(&ifdata, 0, sizeof(ifdata)); /* set interface name */ - strcpy(ifdata.ifr_name, ifname); + strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name)); /* initialize ethtool interface */ memset(&ecmd, 0, sizeof(ecmd)); diff --git a/zebra/interface.c b/zebra/interface.c index c17e408ea0..12312ff43a 100644 --- a/zebra/interface.c +++ b/zebra/interface.c @@ -493,7 +493,7 @@ void if_add_update(struct interface *ifp) if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { SET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE); - if (if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON) { + if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON) { if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug( "interface %s vrf %u index %d is shutdown. " diff --git a/zebra/ioctl.c b/zebra/ioctl.c index 835f1f4934..4404bb4483 100644 --- a/zebra/ioctl.c +++ b/zebra/ioctl.c @@ -44,7 +44,7 @@ extern struct zebra_privs_t zserv_privs; /* clear and set interface name string */ void ifreq_set_name(struct ifreq *ifreq, struct interface *ifp) { - strncpy(ifreq->ifr_name, ifp->name, IFNAMSIZ); + strlcpy(ifreq->ifr_name, ifp->name, sizeof(ifreq->ifr_name)); } /* call ioctl system call */ diff --git a/zebra/irdp_packet.c b/zebra/irdp_packet.c index 0832245536..11bc9b0440 100644 --- a/zebra/irdp_packet.c +++ b/zebra/irdp_packet.c @@ -182,6 +182,7 @@ static int irdp_recvmsg(int sock, u_char *buf, int size, int *ifindex) char adata[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())]; int ret; + memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)0; msg.msg_namelen = 0; msg.msg_iov = &iov; diff --git a/zebra/rt_netlink.c b/zebra/rt_netlink.c index e28fe5630a..c2e8349bf5 100644 --- a/zebra/rt_netlink.c +++ b/zebra/rt_netlink.c @@ -558,8 +558,8 @@ static int netlink_route_change_read_multicast(struct sockaddr_nl *snl, if (IS_ZEBRA_DEBUG_KERNEL) { struct interface *ifp; - strcpy(sbuf, inet_ntoa(m->sg.src)); - strcpy(gbuf, inet_ntoa(m->sg.grp)); + strlcpy(sbuf, inet_ntoa(m->sg.src), sizeof(sbuf)); + strlcpy(gbuf, inet_ntoa(m->sg.grp), sizeof(gbuf)); for (count = 0; count < oif_count; count++) { ifp = if_lookup_by_index(oif[count], vrf); char temp[256]; @@ -1553,6 +1553,8 @@ int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in) int kernel_route_rib(struct prefix *p, struct prefix *src_p, struct route_entry *old, struct route_entry *new) { + assert(old || new); + if (!old && new) return netlink_route_multipath(RTM_NEWROUTE, p, src_p, new, 0); if (old && !new) diff --git a/zebra/rtadv.c b/zebra/rtadv.c index 2182d6618c..295975c5ca 100644 --- a/zebra/rtadv.c +++ b/zebra/rtadv.c @@ -107,6 +107,7 @@ static int rtadv_recv_packet(struct zebra_ns *zns, int sock, u_char *buf, char adata[1024]; /* Fill in message and iovec. */ + memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)from; msg.msg_namelen = sizeof(struct sockaddr_in6); msg.msg_iov = &iov; diff --git a/zebra/zebra_mpls.c b/zebra/zebra_mpls.c index cee3a03858..3c4de40db3 100644 --- a/zebra/zebra_mpls.c +++ b/zebra/zebra_mpls.c @@ -840,7 +840,7 @@ static void lsp_schedule(struct hash_backet *backet, void *ctxt) zebra_lsp_t *lsp; lsp = (zebra_lsp_t *)backet->data; - lsp_processq_add(lsp); + (void)lsp_processq_add(lsp); } /* @@ -2718,10 +2718,10 @@ int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf) for (ALL_LIST_ELEMENTS_RO(slsp_list, node, slsp)) { for (snhlfe = slsp->snhlfe_list; snhlfe; snhlfe = snhlfe->next) { - char buf[INET6_ADDRSTRLEN]; + char buf[BUFSIZ]; char lstr[30]; - snhlfe2str(snhlfe, buf, BUFSIZ); + snhlfe2str(snhlfe, buf, sizeof(buf)); switch (snhlfe->out_label) { case MPLS_V4_EXP_NULL_LABEL: case MPLS_V6_EXP_NULL_LABEL: diff --git a/zebra/zebra_mroute.c b/zebra/zebra_mroute.c index c4d674df23..75d5d5d627 100644 --- a/zebra/zebra_mroute.c +++ b/zebra/zebra_mroute.c @@ -48,8 +48,8 @@ int zebra_ipmr_route_stats(struct zserv *client, int fd, u_short length, char sbuf[40]; char gbuf[40]; - strcpy(sbuf, inet_ntoa(mroute.sg.src)); - strcpy(gbuf, inet_ntoa(mroute.sg.grp)); + strlcpy(sbuf, inet_ntoa(mroute.sg.src), sizeof(sbuf)); + strlcpy(gbuf, inet_ntoa(mroute.sg.grp), sizeof(gbuf)); zlog_debug("Asking for (%s,%s) mroute information", sbuf, gbuf); } diff --git a/zebra/zebra_pw.c b/zebra/zebra_pw.c index 0b6263fac5..ce9f19c3cc 100644 --- a/zebra/zebra_pw.c +++ b/zebra/zebra_pw.c @@ -328,6 +328,7 @@ DEFUN_NOSH (pseudowire_if, if (!pw) return CMD_SUCCESS; zebra_pw_del(zvrf, pw); + return CMD_SUCCESS; } if (!pw) diff --git a/zebra/zebra_vrf.c b/zebra/zebra_vrf.c index ff140bad67..82c0524a8f 100644 --- a/zebra/zebra_vrf.c +++ b/zebra/zebra_vrf.c @@ -470,7 +470,7 @@ static int vrf_config_write(struct vty *vty) RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) { zvrf = vrf->info; - if (!zvrf || strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) { + if (strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) { vty_out(vty, "vrf %s\n", zvrf_name(zvrf)); vty_out(vty, "!\n"); } diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index 9f887e8401..7649d5f358 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -1181,7 +1181,10 @@ DEFUN (show_ip_route, tag = strtoul(argv[idx + 1]->arg, NULL, 10); else if (argv_find(argv, argc, "A.B.C.D/M", &idx)) { - str2prefix(argv[idx]->arg, &p); + if (str2prefix(argv[idx]->arg, &p) <= 0) { + vty_out(vty, "%% Malformed prefix\n"); + return CMD_WARNING; + } longer_prefixes = true; } @@ -1834,7 +1837,10 @@ DEFUN (show_ipv6_route, tag = strtoul(argv[idx + 1]->arg, NULL, 10); else if (argv_find(argv, argc, "X:X::X:X/M", &idx)) { - str2prefix(argv[idx]->arg, &p); + if (str2prefix(argv[idx]->arg, &p) <= 0) { + vty_out(vty, "%% Malformed prefix\n"); + return CMD_WARNING; + } longer_prefixes = true; } @@ -2572,6 +2578,8 @@ DEFUN (ip_zebra_import_table_distance, vty_out(vty, "Invalid routing table ID, %d. Must be in range 1-252\n", table_id); + if (rmap) + XFREE(MTYPE_ROUTE_MAP_NAME, rmap); return CMD_WARNING; } @@ -2579,6 +2587,8 @@ DEFUN (ip_zebra_import_table_distance, vty_out(vty, "Invalid routing table ID, %d. Must be non-default table\n", table_id); + if (rmap) + XFREE(MTYPE_ROUTE_MAP_NAME, rmap); return CMD_WARNING; } diff --git a/zebra/zebra_vxlan.c b/zebra/zebra_vxlan.c index f99c16ae91..b20ba7d9f5 100644 --- a/zebra/zebra_vxlan.c +++ b/zebra/zebra_vxlan.c @@ -1343,8 +1343,7 @@ static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni, zlog_err( "%u:Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u", ifp->vrf_id, ipaddr2str(ip, buf2, sizeof(buf2)), - prefix_mac2str(macaddr, NULL, - ETHER_ADDR_STRLEN), + prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name, ifp->ifindex, zvni->vni); return -1; } @@ -1380,6 +1379,7 @@ static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni, struct zebra_vrf *zvrf = NULL; zebra_neigh_t *n = NULL; zebra_mac_t *mac = NULL; + char buf1[ETHER_ADDR_STRLEN]; char buf2[INET6_ADDRSTRLEN]; zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id); @@ -1393,11 +1393,13 @@ static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni, /* mac entry should be present */ mac = zvni_mac_lookup(zvni, &n->emac); - if (!mac) - zlog_err("%u: MAC %s doesnt exsists for neigh %s on VNI %u", + if (!mac) { + zlog_err("%u: MAC %s doesnt exists for neigh %s on VNI %u", ifp->vrf_id, - prefix_mac2str(&n->emac, NULL, ETHER_ADDR_STRLEN), + prefix_mac2str(&n->emac, buf1, sizeof(buf1)), ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni); + return -1; + } /* If the entry is not local nothing to do*/ if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) @@ -1407,7 +1409,7 @@ static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni, zlog_debug( "%u:SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP", ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni, - prefix_mac2str(&(n->emac), NULL, ETHER_ADDR_STRLEN), + prefix_mac2str(&(n->emac), buf1, sizeof(buf1)), ipaddr2str(ip, buf2, sizeof(buf2))); /* Remove neighbor from BGP. */