1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

netdev2_ieee802154: replace overflow checks with asserts

This commit is contained in:
Martine Lenders 2016-04-17 14:34:23 +02:00 committed by Martine Lenders
parent 8a85725cf9
commit 315dea3e7c

View File

@ -32,9 +32,7 @@ static int _get_iid(netdev2_ieee802154_t *dev, eui64_t *value, size_t max_len)
uint8_t *addr; uint8_t *addr;
uint16_t addr_len; uint16_t addr_len;
if (max_len < sizeof(eui64_t)) { assert(max_len >= sizeof(eui64_t));
return -EOVERFLOW;
}
if (dev->flags & NETDEV2_IEEE802154_SRC_MODE_LONG) { if (dev->flags & NETDEV2_IEEE802154_SRC_MODE_LONG) {
addr_len = IEEE802154_LONG_ADDRESS_LEN; addr_len = IEEE802154_LONG_ADDRESS_LEN;
@ -56,27 +54,18 @@ int netdev2_ieee802154_get(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
switch (opt) { switch (opt) {
case NETOPT_ADDRESS: case NETOPT_ADDRESS:
if (max_len < sizeof(dev->short_addr)) { assert(max_len >= sizeof(dev->short_addr));
res = -EOVERFLOW;
break;
}
memcpy(value, dev->short_addr, sizeof(dev->short_addr)); memcpy(value, dev->short_addr, sizeof(dev->short_addr));
res = sizeof(dev->short_addr); res = sizeof(dev->short_addr);
break; break;
case NETOPT_ADDRESS_LONG: case NETOPT_ADDRESS_LONG:
if (max_len < sizeof(dev->long_addr)) { assert(max_len >= sizeof(dev->long_addr));
res = -EOVERFLOW;
break;
}
memcpy(value, dev->long_addr, sizeof(dev->long_addr)); memcpy(value, dev->long_addr, sizeof(dev->long_addr));
res = sizeof(dev->long_addr); res = sizeof(dev->long_addr);
break; break;
case NETOPT_ADDR_LEN: case NETOPT_ADDR_LEN:
case NETOPT_SRC_LEN: case NETOPT_SRC_LEN:
if (max_len < sizeof(uint16_t)) { assert(max_len == sizeof(uint16_t));
res = -EOVERFLOW;
break;
}
if (dev->flags & NETDEV2_IEEE802154_SRC_MODE_LONG) { if (dev->flags & NETDEV2_IEEE802154_SRC_MODE_LONG) {
*((uint16_t *)value) = IEEE802154_LONG_ADDRESS_LEN; *((uint16_t *)value) = IEEE802154_LONG_ADDRESS_LEN;
} }
@ -86,26 +75,17 @@ int netdev2_ieee802154_get(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
res = sizeof(uint16_t); res = sizeof(uint16_t);
break; break;
case NETOPT_NID: case NETOPT_NID:
if (max_len < sizeof(dev->pan)) { assert(max_len == sizeof(dev->pan));
res = -EOVERFLOW;
break;
}
*((uint16_t *)value) = dev->pan; *((uint16_t *)value) = dev->pan;
res = sizeof(dev->pan); res = sizeof(dev->pan);
break; break;
case NETOPT_CHANNEL: case NETOPT_CHANNEL:
if (max_len < sizeof(uint16_t)) { assert(max_len == sizeof(uint16_t));
res = -EOVERFLOW;
break;
}
*((uint16_t *)value) = (uint16_t)dev->chan; *((uint16_t *)value) = (uint16_t)dev->chan;
res = sizeof(dev->chan); res = sizeof(dev->chan);
break; break;
case NETOPT_AUTOACK: case NETOPT_AUTOACK:
if (max_len < sizeof(netopt_enable_t)) { assert(max_len == sizeof(netopt_enable_t));
res = -EOVERFLOW;
break;
}
if (dev->flags & NETDEV2_IEEE802154_ACK_REQ) { if (dev->flags & NETDEV2_IEEE802154_ACK_REQ) {
*((netopt_enable_t *)value) = NETOPT_ENABLE; *((netopt_enable_t *)value) = NETOPT_ENABLE;
} }
@ -115,10 +95,7 @@ int netdev2_ieee802154_get(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
res = sizeof(netopt_enable_t); res = sizeof(netopt_enable_t);
break; break;
case NETOPT_RAWMODE: case NETOPT_RAWMODE:
if (max_len < sizeof(netopt_enable_t)) { assert(max_len == sizeof(netopt_enable_t));
res = -EOVERFLOW;
break;
}
if (dev->flags & NETDEV2_IEEE802154_RAW) { if (dev->flags & NETDEV2_IEEE802154_RAW) {
*((netopt_enable_t *)value) = NETOPT_ENABLE; *((netopt_enable_t *)value) = NETOPT_ENABLE;
} }
@ -129,19 +106,13 @@ int netdev2_ieee802154_get(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
break; break;
#ifdef MODULE_GNRC #ifdef MODULE_GNRC
case NETOPT_PROTO: case NETOPT_PROTO:
if (max_len < sizeof(gnrc_nettype_t)) { assert(max_len == sizeof(gnrc_nettype_t));
res = -EOVERFLOW;
break;
}
*((gnrc_nettype_t *)value) = dev->proto; *((gnrc_nettype_t *)value) = dev->proto;
res = sizeof(gnrc_nettype_t); res = sizeof(gnrc_nettype_t);
break; break;
#endif #endif
case NETOPT_DEVICE_TYPE: case NETOPT_DEVICE_TYPE:
if (max_len < sizeof(uint16_t)) { assert(max_len == sizeof(uint16_t));
res = -EOVERFLOW;
break;
}
*((uint16_t *)value) = NETDEV2_TYPE_IEEE802154; *((uint16_t *)value) = NETDEV2_TYPE_IEEE802154;
res = sizeof(uint16_t); res = sizeof(uint16_t);
break; break;
@ -150,10 +121,7 @@ int netdev2_ieee802154_get(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
break; break;
#ifdef MODULE_NETSTATS_L2 #ifdef MODULE_NETSTATS_L2
case NETOPT_STATS: case NETOPT_STATS:
if (max_len < sizeof(uintptr_t)) { assert(max_len == sizeof(uintptr_t));
res = -EOVERFLOW;
break;
}
*((netstats_t **)value) = &dev->netdev.stats; *((netstats_t **)value) = &dev->netdev.stats;
res = sizeof(uintptr_t); res = sizeof(uintptr_t);
break; break;
@ -172,10 +140,7 @@ int netdev2_ieee802154_set(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
switch (opt) { switch (opt) {
case NETOPT_CHANNEL: case NETOPT_CHANNEL:
{ {
if (len > sizeof(uint16_t)) { assert(len == sizeof(uint16_t));
res = -EOVERFLOW;
break;
}
uint16_t chan = *((uint16_t *)value); uint16_t chan = *((uint16_t *)value);
/* real validity needs to be checked by device, since sub-GHz and /* real validity needs to be checked by device, since sub-GHz and
* 2.4 GHz band radios have different legal values. Here we only * 2.4 GHz band radios have different legal values. Here we only
@ -186,29 +151,20 @@ int netdev2_ieee802154_set(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
break; break;
} }
case NETOPT_ADDRESS: case NETOPT_ADDRESS:
if (len > sizeof(dev->short_addr)) { assert(len <= sizeof(dev->short_addr));
res = -EOVERFLOW;
break;
}
memset(dev->short_addr, 0, sizeof(dev->short_addr)); memset(dev->short_addr, 0, sizeof(dev->short_addr));
memcpy(dev->short_addr, value, len); memcpy(dev->short_addr, value, len);
res = sizeof(dev->short_addr); res = sizeof(dev->short_addr);
break; break;
case NETOPT_ADDRESS_LONG: case NETOPT_ADDRESS_LONG:
if (len > sizeof(dev->long_addr)) { assert(len <= sizeof(dev->long_addr));
res = -EOVERFLOW;
break;
}
memset(dev->long_addr, 0, sizeof(dev->long_addr)); memset(dev->long_addr, 0, sizeof(dev->long_addr));
memcpy(dev->long_addr, value, len); memcpy(dev->long_addr, value, len);
res = sizeof(dev->long_addr); res = sizeof(dev->long_addr);
break; break;
case NETOPT_ADDR_LEN: case NETOPT_ADDR_LEN:
case NETOPT_SRC_LEN: case NETOPT_SRC_LEN:
if (len > sizeof(uint16_t)) { assert(len == sizeof(uint16_t));
res = -EOVERFLOW;
break;
}
switch ((*(uint16_t *)value)) { switch ((*(uint16_t *)value)) {
case IEEE802154_SHORT_ADDRESS_LEN: case IEEE802154_SHORT_ADDRESS_LEN:
dev->flags &= ~NETDEV2_IEEE802154_SRC_MODE_LONG; dev->flags &= ~NETDEV2_IEEE802154_SRC_MODE_LONG;
@ -223,10 +179,7 @@ int netdev2_ieee802154_set(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
res = sizeof(uint16_t); res = sizeof(uint16_t);
break; break;
case NETOPT_NID: case NETOPT_NID:
if (len > sizeof(uint16_t)) { assert(len == sizeof(dev->pan));
res = -EOVERFLOW;
break;
}
dev->pan = *((uint16_t *)value); dev->pan = *((uint16_t *)value);
res = sizeof(dev->pan); res = sizeof(dev->pan);
break; break;
@ -250,10 +203,7 @@ int netdev2_ieee802154_set(netdev2_ieee802154_t *dev, netopt_t opt, void *value,
break; break;
#ifdef MODULE_GNRC #ifdef MODULE_GNRC
case NETOPT_PROTO: case NETOPT_PROTO:
if (len > sizeof(gnrc_nettype_t)) { assert(len == sizeof(gnrc_nettype_t));
res = -EOVERFLOW;
break;
}
dev->proto = *((gnrc_nettype_t *)value); dev->proto = *((gnrc_nettype_t *)value);
res = sizeof(gnrc_nettype_t); res = sizeof(gnrc_nettype_t);
break; break;