Skip to content

Commit

Permalink
protocols: don't use assert on paths that can be reached
Browse files Browse the repository at this point in the history
Malformed packets should not make lldpd crash. Ensure we can handle them
by not using assert() in this part.
  • Loading branch information
vincentbernat committed Oct 4, 2015
1 parent ff75b38 commit 9221b5c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/daemon/lldpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface)
return NULL;
}
mgmt->m_family = family;
assert(addrsize <= LLDPD_MGMT_MAXADDRSIZE);
memcpy(&mgmt->m_addr, addrptr, addrsize);
mgmt->m_addrsize = addrsize;
mgmt->m_iface = iface;
Expand Down
10 changes: 7 additions & 3 deletions src/daemon/protocols/cdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <assert.h>

static int
cdp_send(struct lldpd *global,
Expand Down Expand Up @@ -438,8 +437,13 @@ cdp_decode(struct lldpd *cfg, char *frame, int s,
mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, &addr,
sizeof(struct in_addr), 0);
if (mgmt == NULL) {
assert(errno == ENOMEM);
log_warn("cdp", "unable to allocate memory for management address");
if (errno == ENOMEM)
log_warn("cdp",
"unable to allocate memory for management address");
else
log_warn("cdp",
"too large management address received on %s",
hardware->h_ifname);
goto malformed;
}
TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries);
Expand Down
1 change: 0 additions & 1 deletion src/daemon/protocols/edp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <errno.h>
#include <arpa/inet.h>
#include <fnmatch.h>
#include <assert.h>

static int seq = 0;

Expand Down
12 changes: 7 additions & 5 deletions src/daemon/protocols/lldp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -161,7 +160,7 @@ static int _lldp_send(struct lldpd *global,
/* Management addresses */
TAILQ_FOREACH(mgmt, &chassis->c_mgmt, m_entries) {
proto = lldpd_af_to_lldp_proto(mgmt->m_family);
assert(proto != LLDP_MGMT_ADDR_NONE);
if (proto == LLDP_MGMT_ADDR_NONE) continue;
if (!(
POKE_START_LLDP_TLV(LLDP_TLV_MGMT_ADDR) &&
/* Size of the address, including its type */
Expand Down Expand Up @@ -749,9 +748,12 @@ lldp_decode(struct lldpd *cfg, char *frame, int s,
iface = 0;
mgmt = lldpd_alloc_mgmt(af, addr_ptr, addr_length, iface);
if (mgmt == NULL) {
assert(errno == ENOMEM);
log_warn("lldp", "unable to allocate memory "
"for management address");
if (errno == ENOMEM)
log_warn("lldp", "unable to allocate memory "
"for management address");
else
log_warn("lldp", "too large management address "
"received on %s", hardware->h_ifname);
goto malformed;
}
TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries);
Expand Down
8 changes: 5 additions & 3 deletions src/daemon/protocols/sonmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <assert.h>

static struct sonmp_chassis sonmp_chassis_types[] = {
{1, "unknown (via SONMP)"},
Expand Down Expand Up @@ -358,8 +357,11 @@ sonmp_decode(struct lldpd *cfg, char *frame, int s,
}
mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, &address, sizeof(struct in_addr), 0);
if (mgmt == NULL) {
assert(errno == ENOMEM);
log_warn("sonmp", "unable to allocate memory for management address");
if (errno == ENOMEM)
log_warn("sonmp", "unable to allocate memory for management address");
else
log_warn("sonmp", "too large management address received on %s",
hardware->h_ifname);
goto malformed;
}
TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries);
Expand Down

0 comments on commit 9221b5c

Please sign in to comment.