Loop-unroll in CoAP for fixing some potential bugs on some platforms

where size_t is not the same as unsigned int.
This commit is contained in:
Joakim Eriksson 2015-04-30 12:16:04 +02:00 committed by Niclas Finne
parent 70b3585f76
commit 1e0b5292d7

View file

@ -113,17 +113,19 @@ coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length); buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
/* avoids code duplication without function overhead */ if(delta > 268) {
unsigned int *x = &delta; buffer[++written] = ((delta - 269) >> 8) & 0xff;
buffer[++written] = (delta - 269) & 0xff;
do { } else if(delta > 12) {
if(*x > 268) { buffer[++written] = (delta - 13);
buffer[++written] = (*x - 269) >> 8; }
buffer[++written] = (*x - 269);
} else if(*x > 12) { if(length > 268) {
buffer[++written] = (*x - 13); buffer[++written] = ((length - 269) >> 8) & 0xff;
buffer[++written] = (length - 269) & 0xff;
} else if(length > 12) {
buffer[++written] = (length - 13);
} }
} while(x != &length && (x = &length));
PRINTF("WRITTEN %u B opt header\n", 1 + written); PRINTF("WRITTEN %u B opt header\n", 1 + written);
@ -500,25 +502,31 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
option_length = current_option[0] & 0x0F; option_length = current_option[0] & 0x0F;
++current_option; ++current_option;
/* avoids code duplication without function overhead */ if(option_delta == 13) {
unsigned int *x = &option_delta; option_delta += current_option[0];
do {
if(*x == 13) {
*x += current_option[0];
++current_option; ++current_option;
} else if(*x == 14) { } else if(option_delta == 14) {
*x += 255; option_delta += 255;
*x += current_option[0] << 8; option_delta += current_option[0] << 8;
++current_option; ++current_option;
*x += current_option[0]; option_delta += current_option[0];
++current_option;
}
if(option_length == 13) {
option_length += current_option[0];
++current_option;
} else if(option_length == 14) {
option_length += 255;
option_length += current_option[0] << 8;
++current_option;
option_length += current_option[0];
++current_option; ++current_option;
} }
} while(x != &option_length && (x = &option_length));
option_number += option_delta; option_number += option_delta;
PRINTF("OPTION %u (delta %u, len %u): ", option_number, option_delta, PRINTF("OPTION %u (delta %u, len %zu): ", option_number, option_delta,
option_length); option_length);
SET_OPTION(coap_pkt, option_number); SET_OPTION(coap_pkt, option_number);