Skip to content

Commit

Permalink
net: coap: Fix Coap coap_option_value_to_int and coap_append_option_int
Browse files Browse the repository at this point in the history
In coap_option_value_to_int function, when coap option length is 4,
option->value[3] should be shifted by 0 rather than option->value[2].
This doesn't affect the behavior of function but needs to be fixed.

In coap_append_option_int function, when the value is between 0xffff
and 0xffffff(when option length is 3), bit shift operation is wrong.
For example, if the value is 0xABCDEF, by sys_put_be16(val, data)
data[0]=0xCD, data[1]=0xEF, by val >> 16, data[2]=0xAB. So the result
becomes 0xCDEFAB not 0xABCDEF. So, to sys_put_be16 function hand
&data[1] over instead of handing data over and val >> 16 needs to be
set to data[0], not data[2].

Signed-off-by: Taehwa Kang <hegrecomm@gmail.com>
  • Loading branch information
hegrecom authored and jukkar committed Sep 28, 2018
1 parent 10c6a0c commit 2bdc592
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions subsys/net/lib/coap/coap.c
Expand Up @@ -705,7 +705,7 @@ unsigned int coap_option_value_to_int(const struct coap_option *option)
return (option->value[2] << 0) | (option->value[1] << 8) |
(option->value[0] << 16);
case 4:
return (option->value[2] << 0) | (option->value[2] << 8) |
return (option->value[3] << 0) | (option->value[2] << 8) |
(option->value[1] << 16) | (option->value[0] << 24);
default:
return 0;
Expand Down Expand Up @@ -1060,8 +1060,8 @@ int coap_append_option_int(struct coap_packet *cpkt, u16_t code,
sys_put_be16(val, data);
len = 2;
} else if (val < 0xFFFFFF) {
sys_put_be16(val, data);
data[2] = val >> 16;
sys_put_be16(val, &data[1]);
data[0] = val >> 16;
len = 3;
} else {
sys_put_be32(val, data);
Expand Down

0 comments on commit 2bdc592

Please sign in to comment.