Skip to content

Commit ba6784c

Browse files
committed
sys/net/unicoap: properly handle options with value zero
1 parent 217598c commit ba6784c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

sys/net/application_layer/unicoap/options.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ ssize_t unicoap_options_copy_value(const unicoap_options_t* options, unicoap_opt
546546
if (size < 0) {
547547
return size;
548548
}
549+
if (size == 0) {
550+
/* value 0 SHOULD have been encoded as zero-length value, see RFC7252, Section 3.2 */
551+
*dest = 0;
552+
return size;
553+
}
549554
if ((size_t)size > capacity) {
550555
return -ENOBUFS;
551556
}
@@ -923,6 +928,11 @@ ssize_t _unicoap_options_get_variable_uint(const unicoap_options_t* options,
923928
if (size < 0) {
924929
return size;
925930
}
931+
if (size == 0) {
932+
/* value 0 SHOULD have been encoded as zero-length value, see RFC7252, Section 3.2 */
933+
*uint = 0;
934+
return size;
935+
}
926936
if ((size_t)size > max_size) {
927937
return -EBADOPT;
928938
}

tests/unittests/tests-unicoap/tests-unicoap-options.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,26 @@ static void test_remove_multiple(void)
383383
_TEST_ASSERT_EQUAL_BYTES(options_blob, unicoap_options_data(&options), sizeof(options_blob));
384384
}
385385

386+
static void test_shortest_uint_in_option_value(void)
387+
{
388+
static const uint8_t options_blob[] = { 0x00 };
389+
const unicoap_option_number_t OPTION_NUMBER = 0; // RESERVED
390+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 10);
391+
TEST_ASSERT_EQUAL_INT(0, unicoap_options_add_uint(&options, OPTION_NUMBER, 0));
392+
TEST_ASSERT_EQUAL_INT(sizeof(options_blob), unicoap_options_size(&options));
393+
_TEST_ASSERT_EQUAL_BYTES(options_blob, unicoap_options_data(&options), sizeof(options_blob));
394+
395+
uint8_t uint8_val = 42;
396+
TEST_ASSERT_EQUAL_INT(0, unicoap_options_get_uint8(&options, OPTION_NUMBER, &uint8_val));
397+
TEST_ASSERT_EQUAL_INT(0, uint8_val);
398+
uint16_t uint16_val = 42;
399+
TEST_ASSERT_EQUAL_INT(0, unicoap_options_get_uint16(&options, OPTION_NUMBER, &uint16_val));
400+
TEST_ASSERT_EQUAL_INT(0, uint16_val);
401+
uint32_t uint32_val = 42;
402+
TEST_ASSERT_EQUAL_INT(0, unicoap_options_get_uint32(&options, OPTION_NUMBER, &uint32_val));
403+
TEST_ASSERT_EQUAL_INT(0, uint32_val);
404+
}
405+
386406
Test* tests_unicoap_options(void)
387407
{
388408
EMB_UNIT_TESTFIXTURES(fixtures){
@@ -393,6 +413,7 @@ Test* tests_unicoap_options(void)
393413
new_TestFixture(test_remove_leading),
394414
new_TestFixture(test_remove_trailing),
395415
new_TestFixture(test_remove_multiple),
416+
new_TestFixture(test_shortest_uint_in_option_value),
396417
};
397418

398419
EMB_UNIT_TESTCALLER(test_unicoap, NULL, NULL, fixtures);

0 commit comments

Comments
 (0)