Defensive programming: make sure that we don't fail completely if we get a callback for a NULL pointer

This commit is contained in:
Adam Dunkels 2013-03-17 23:38:14 +01:00
parent 82b755c017
commit f2fbb4b49d
1 changed files with 135 additions and 134 deletions

View File

@ -186,15 +186,19 @@ free_first_packet(struct neighbor_queue *n)
static void
packet_sent(void *ptr, int status, int num_transmissions)
{
struct neighbor_queue *n = ptr;
struct rdc_buf_list *q = list_head(n->queued_packet_list);
struct qbuf_metadata *metadata = (struct qbuf_metadata *)q->ptr;
struct neighbor_queue *n;
struct rdc_buf_list *q;
struct qbuf_metadata *metadata;
clock_time_t time = 0;
mac_callback_t sent;
void *cptr;
int num_tx;
int backoff_transmissions;
n = ptr;
if(n == NULL) {
return;
}
switch(status) {
case MAC_TX_OK:
case MAC_TX_NOACK:
@ -208,15 +212,19 @@ packet_sent(void *ptr, int status, int num_transmissions)
break;
}
q = list_head(n->queued_packet_list);
if(q != NULL) {
metadata = (struct qbuf_metadata *)q->ptr;
if(metadata != NULL) {
sent = metadata->sent;
cptr = metadata->cptr;
num_tx = n->transmissions;
if(status == MAC_TX_COLLISION ||
status == MAC_TX_NOACK) {
/* If the transmission was not performed because of a collision or
noack, we must retransmit the packet. */
/* If the transmission was not performed because of a
collision or noack, we must retransmit the packet. */
switch(status) {
case MAC_TX_COLLISION:
@ -269,6 +277,8 @@ packet_sent(void *ptr, int status, int num_transmissions)
free_first_packet(n);
mac_call_sent_callback(sent, cptr, status, num_tx);
}
}
}
}
/*---------------------------------------------------------------------------*/
static void
@ -277,15 +287,10 @@ send_packet(mac_callback_t sent, void *ptr)
struct rdc_buf_list *q;
struct neighbor_queue *n;
static uint16_t seqno;
const rimeaddr_t *addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, seqno++);
/* If the packet is a broadcast, do not allocate a queue
entry. Instead, just send it out. */
if(!rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
&rimeaddr_null)) {
const rimeaddr_t *addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
/* Look for the neighbor entry */
n = neighbor_queue_from_addr(addr);
if(n == NULL) {
@ -353,10 +358,6 @@ send_packet(mac_callback_t sent, void *ptr)
PRINTF("csma: could not allocate neighbor, dropping packet\n");
}
mac_call_sent_callback(sent, ptr, MAC_TX_ERR, 1);
} else {
PRINTF("csma: send broadcast\n");
NETSTACK_RDC.send(sent, ptr);
}
}
/*---------------------------------------------------------------------------*/
static void