You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2026/10/27 09:39:17 UTC
(mynewt-core) branch master updated (5c3c8bace -> 074d7ef19)
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
from 5c3c8bace tinyusb/msc_fat_view: Handle config lines split between sectors
new c02c0f791 net/oic: Fix coding style in oc_ri_process_discovery_payload
new 436f5ba54 net/oic: Fix payload parsing in oc_parse_rep
new 756a88782 net/oic: Check rep type in discovery payload processing
new 074d7ef19 net/oic: Add tests for malformed payload parsing
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
net/oic/selftest/src/test_parse.c | 139 +++++++++++
net/oic/src/api/oc_discovery.c | 246 +++++++++---------
net/oic/src/api/oc_rep.c | 509 ++++++++++++++++++++++++++------------
3 files changed, 607 insertions(+), 287 deletions(-)
(mynewt-core) 03/04: net/oic: Check rep type in discovery payload processing
Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 756a88782c73ab2443a3cb5b57fb3cf223a93d20
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 20:45:58 2026 +0100
net/oic: Check rep type in discovery payload processing
Elements of parsed discovery payload were assumed to be objects and
value_object was used without checking the type. For payload with
root map, value of e.g. integer property was used as pointer.
Skip elements that are not objects.
Assisted-by: Claude:claude-opus-5-5
---
net/oic/src/api/oc_discovery.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/net/oic/src/api/oc_discovery.c b/net/oic/src/api/oc_discovery.c
index 2f40481d7..39213c876 100644
--- a/net/oic/src/api/oc_discovery.c
+++ b/net/oic/src/api/oc_discovery.c
@@ -209,6 +209,10 @@ oc_ri_process_discovery_payload(struct coap_packet_rx *rsp,
if (s == 0)
array = rep;
while (array != NULL) {
+ if (array->type != OBJECT) {
+ array = array->next;
+ continue;
+ }
oc_rep_t *device_map = array->value_object;
while (device_map != NULL) {
switch (device_map->type) {
(mynewt-core) 01/04: net/oic: Fix coding style in oc_ri_process_discovery_payload
Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit c02c0f791ccf2ddfcc1fc0dac160b49c3af6aff6
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 20:45:32 2026 +0100
net/oic: Fix coding style in oc_ri_process_discovery_payload
Reformat with clang-format before modifying it. No functional changes.
Assisted-by: Claude:claude-opus-5-5
---
net/oic/src/api/oc_discovery.c | 242 ++++++++++++++++++++---------------------
1 file changed, 120 insertions(+), 122 deletions(-)
diff --git a/net/oic/src/api/oc_discovery.c b/net/oic/src/api/oc_discovery.c
index b00b33c2f..2f40481d7 100644
--- a/net/oic/src/api/oc_discovery.c
+++ b/net/oic/src/api/oc_discovery.c
@@ -182,140 +182,138 @@ oc_create_discovery_resource(void)
#if MYNEWT_VAL(OC_CLIENT_DISCOVERY_ENABLE)
oc_discovery_flags_t
oc_ri_process_discovery_payload(struct coap_packet_rx *rsp,
- oc_discovery_cb_t *handler,
- oc_endpoint_t *endpoint)
+ oc_discovery_cb_t *handler, oc_endpoint_t *endpoint)
{
- oc_discovery_flags_t ret = OC_CONTINUE_DISCOVERY;
- oc_string_t uri = {
- .os_sz = 0,
- .os_str = NULL
- };
- oc_string_t di = {
- .os_sz = 0,
- .os_str = NULL
- };
- bool secure = false;
- uint16_t dtls_port = 0, default_port = 0;
- oc_string_array_t types = {};
- oc_interface_mask_t interfaces = 0;
- oc_server_handle_t handle;
- uint16_t data_off;
- struct os_mbuf *m;
- int len;
- struct oc_endpoint_ip *ip;
+ oc_discovery_flags_t ret = OC_CONTINUE_DISCOVERY;
+ oc_string_t uri = { .os_sz = 0, .os_str = NULL };
+ oc_string_t di = { .os_sz = 0, .os_str = NULL };
+ bool secure = false;
+ uint16_t dtls_port = 0, default_port = 0;
+ oc_string_array_t types = {};
+ oc_interface_mask_t interfaces = 0;
+ oc_server_handle_t handle;
+ uint16_t data_off;
+ struct os_mbuf *m;
+ int len;
+ struct oc_endpoint_ip *ip;
- memcpy(&handle.endpoint, endpoint, oc_endpoint_size(endpoint));
- if (oc_endpoint_is_ip(endpoint)) {
- default_port = ((struct oc_endpoint_ip *)endpoint)->port;
- }
+ memcpy(&handle.endpoint, endpoint, oc_endpoint_size(endpoint));
+ if (oc_endpoint_is_ip(endpoint)) {
+ default_port = ((struct oc_endpoint_ip *)endpoint)->port;
+ }
- oc_rep_t *array = 0, *rep;
+ oc_rep_t *array = 0, *rep;
- len = coap_get_payload(rsp, &m, &data_off);
- int s = oc_parse_rep(m, data_off, len, &rep);
- if (s == 0)
- array = rep;
- while (array != NULL) {
- oc_rep_t *device_map = array->value_object;
- while (device_map != NULL) {
- switch (device_map->type) {
- case STRING:
- if (oc_string_len(device_map->name) == 2 &&
- strncmp(oc_string(device_map->name), "di", 2) == 0)
- di = device_map->value_string;
- break;
- default:
- break;
- }
- device_map = device_map->next;
- }
- device_map = array->value_object;
- while (device_map != NULL) {
- switch (device_map->type) {
- case OBJECT_ARRAY: {
- oc_rep_t *links = device_map->value_object_array;
- while (links != NULL) {
- switch (links->type) {
- case OBJECT: {
- oc_rep_t *resource_info = links->value_object;
- while (resource_info != NULL) {
- switch (resource_info->type) {
- case STRING:
- uri = resource_info->value_string;
+ len = coap_get_payload(rsp, &m, &data_off);
+ int s = oc_parse_rep(m, data_off, len, &rep);
+ if (s == 0)
+ array = rep;
+ while (array != NULL) {
+ oc_rep_t *device_map = array->value_object;
+ while (device_map != NULL) {
+ switch (device_map->type) {
+ case STRING:
+ if (oc_string_len(device_map->name) == 2 &&
+ strncmp(oc_string(device_map->name), "di", 2) == 0)
+ di = device_map->value_string;
break;
- case STRING_ARRAY:
- if (oc_string_len(resource_info->name) == 2 &&
- strncmp(oc_string(resource_info->name), "rt", 2) == 0)
- types = resource_info->value_array;
- else {
- interfaces = 0;
- int i;
- for (i = 0; i < oc_string_array_get_allocated_size(
- resource_info->value_array);
- i++) {
- interfaces |= oc_ri_get_interface_mask(
- oc_string_array_get_item(resource_info->value_array, i),
- oc_string_array_get_item_size(resource_info->value_array,
- i));
- }
- }
+ default:
break;
- case OBJECT: {
- oc_rep_t *policy_info = resource_info->value_object;
- while (policy_info != NULL) {
- if (policy_info->type == INT &&
- oc_string_len(policy_info->name) == 4 &&
- strncmp(oc_string(policy_info->name), "port", 4) == 0) {
- dtls_port = policy_info->value_int;
- }
- if (policy_info->type == BOOL &&
- oc_string_len(policy_info->name) == 3 &&
- strncmp(oc_string(policy_info->name), "sec", 3) == 0 &&
- policy_info->value_boolean == true) {
- secure = true;
- }
- policy_info = policy_info->next;
- }
- } break;
- default:
- break;
- }
- resource_info = resource_info->next;
}
- if (default_port) {
- ip = (struct oc_endpoint_ip *)&handle.endpoint;
- if (secure) {
- ip->port = dtls_port;
- ip->ep.oe_flags |= OC_ENDPOINT_SECURED;
- } else {
- ip->port = default_port;
- ip->ep.oe_flags &= ~OC_ENDPOINT_SECURED;
+ device_map = device_map->next;
+ }
+ device_map = array->value_object;
+ while (device_map != NULL) {
+ switch (device_map->type) {
+ case OBJECT_ARRAY: {
+ oc_rep_t *links = device_map->value_object_array;
+ while (links != NULL) {
+ switch (links->type) {
+ case OBJECT: {
+ oc_rep_t *resource_info = links->value_object;
+ while (resource_info != NULL) {
+ switch (resource_info->type) {
+ case STRING:
+ uri = resource_info->value_string;
+ break;
+ case STRING_ARRAY:
+ if (oc_string_len(resource_info->name) == 2 &&
+ strncmp(oc_string(resource_info->name),
+ "rt", 2) == 0)
+ types = resource_info->value_array;
+ else {
+ interfaces = 0;
+ int i;
+ for (i = 0;
+ i < oc_string_array_get_allocated_size(
+ resource_info->value_array);
+ i++) {
+ interfaces |= oc_ri_get_interface_mask(
+ oc_string_array_get_item(
+ resource_info->value_array, i),
+ oc_string_array_get_item_size(
+ resource_info->value_array, i));
+ }
+ }
+ break;
+ case OBJECT: {
+ oc_rep_t *policy_info = resource_info->value_object;
+ while (policy_info != NULL) {
+ if (policy_info->type == INT &&
+ oc_string_len(policy_info->name) == 4 &&
+ strncmp(oc_string(policy_info->name),
+ "port", 4) == 0) {
+ dtls_port = policy_info->value_int;
+ }
+ if (policy_info->type == BOOL &&
+ oc_string_len(policy_info->name) == 3 &&
+ strncmp(oc_string(policy_info->name),
+ "sec", 3) == 0 &&
+ policy_info->value_boolean == true) {
+ secure = true;
+ }
+ policy_info = policy_info->next;
+ }
+ } break;
+ default:
+ break;
+ }
+ resource_info = resource_info->next;
+ }
+ if (default_port) {
+ ip = (struct oc_endpoint_ip *)&handle.endpoint;
+ if (secure) {
+ ip->port = dtls_port;
+ ip->ep.oe_flags |= OC_ENDPOINT_SECURED;
+ } else {
+ ip->port = default_port;
+ ip->ep.oe_flags &= ~OC_ENDPOINT_SECURED;
+ }
+ }
+ if (handler(oc_string(di), oc_string(uri), types,
+ interfaces, &handle) == OC_STOP_DISCOVERY) {
+ ret = OC_STOP_DISCOVERY;
+ goto done;
+ }
+ dtls_port = 0;
+ secure = false;
+ } break;
+ default:
+ break;
+ }
+ links = links->next;
}
+ } break;
+ default:
+ break;
}
- if (handler(oc_string(di), oc_string(uri), types, interfaces,
- &handle) == OC_STOP_DISCOVERY) {
- ret = OC_STOP_DISCOVERY;
- goto done;
- }
- dtls_port = 0;
- secure = false;
- } break;
- default:
- break;
- }
- links = links->next;
+ device_map = device_map->next;
}
- } break;
- default:
- break;
- }
- device_map = device_map->next;
+ array = array->next;
}
- array = array->next;
- }
done:
- oc_free_rep(rep);
- return ret;
+ oc_free_rep(rep);
+ return ret;
}
#endif /* OC_CLIENT_DISCOVERY_ENABLE */
#endif /* OC_CLIENT */
(mynewt-core) 04/04: net/oic: Add tests for malformed payload parsing
Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 074d7ef1906ff02e9461f104d1ec5595770fd1de
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 21:37:05 2026 +0100
net/oic: Add tests for malformed payload parsing
Verify that malformed payloads, e.g. with mixed type arrays, too long
strings, non-map root, non-string keys or too deep nesting, are
rejected by oc_parse_rep() without leaking rep objects, and that root
array is parsed as array of objects.
Assisted-by: Claude:claude-opus-5-5
---
net/oic/selftest/src/test_parse.c | 139 ++++++++++++++++++++++++++++++++++++++
1 file changed, 139 insertions(+)
diff --git a/net/oic/selftest/src/test_parse.c b/net/oic/selftest/src/test_parse.c
index 958dca439..5d9c71b3d 100644
--- a/net/oic/selftest/src/test_parse.c
+++ b/net/oic/selftest/src/test_parse.c
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+#include <stdio.h>
#include "os/mynewt.h"
#include <tinycbor/cbor.h>
#include <oic/oc_api.h>
@@ -175,9 +176,147 @@ test_parse_rep_arrays(void)
oc_free_rep(rep);
}
+/* Encodes {"a0": 0, "a1": 1, ...} */
+static size_t
+test_parse_encode_map(int cnt)
+{
+ CborEncoder enc;
+ CborEncoder map;
+ char key[16];
+ int rc;
+ int i;
+
+ cbor_encoder_init(&enc, test_parse_buf, sizeof(test_parse_buf), 0);
+ rc = cbor_encoder_create_map(&enc, &map, cnt);
+ for (i = 0; i < cnt; i++) {
+ snprintf(key, sizeof(key), "a%d", i);
+ rc |= cbor_encode_text_stringz(&map, key);
+ rc |= cbor_encode_int(&map, i);
+ }
+ rc |= cbor_encoder_close_container(&enc, &map);
+ TEST_ASSERT_FATAL(rc == 0);
+
+ return cbor_encoder_get_buffer_size(&enc, test_parse_buf);
+}
+
+static void
+test_parse_rep_invalid(void)
+{
+ /* 1 */
+ static const uint8_t not_map[] = { 0x01 };
+ /* [1] */
+ static const uint8_t not_map_arr[] = { 0x81, 0x01 };
+ /* {"a": ["x...x"]} with string not fitting in array item */
+ static const uint8_t long_str[] = { 0xa1, 0x61, 'a', 0x81, 0x78, 0x18,
+ 'x', 'x', 'x', 'x', 'x', 'x',
+ 'x', 'x', 'x', 'x', 'x', 'x',
+ 'x', 'x', 'x', 'x', 'x', 'x',
+ 'x', 'x', 'x', 'x', 'x', 'x' };
+ /* {"a": [false, "A"]} */
+ static const uint8_t mixed[] = { 0xa1, 0x61, 'a', 0x82, 0xf4, 0x61, 'A' };
+ /* {"a": [1, {}]} */
+ static const uint8_t mixed_map[] = { 0xa1, 0x61, 'a', 0x82, 0x01, 0xa0 };
+ /* {1: 1} */
+ static const uint8_t int_key[] = { 0xa1, 0x01, 0x01 };
+ /* {"a": [1, 2 with 5 elements declared */
+ static const uint8_t truncated[] = { 0xa1, 0x61, 'a', 0x85, 0x01, 0x02 };
+ /* {"a": {"a": {"a": ... 10 levels */
+ static const uint8_t nested[] = { 0xa1, 0x61, 'a', 0xa1, 0x61, 'a', 0xa1,
+ 0x61, 'a', 0xa1, 0x61, 'a', 0xa1, 0x61,
+ 'a', 0xa1, 0x61, 'a', 0xa1, 0x61, 'a',
+ 0xa1, 0x61, 'a', 0xa1, 0x61, 'a', 0xa1,
+ 0x61, 'a', 0xa0 };
+ oc_rep_t dummy;
+ oc_rep_t *rep;
+ size_t len;
+ int rc;
+
+ rep = &dummy;
+ rc = test_parse_rep(not_map, sizeof(not_map), &rep);
+ TEST_ASSERT(rc != 0);
+ TEST_ASSERT(rep == NULL);
+
+ rc = test_parse_rep(not_map_arr, sizeof(not_map_arr), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(long_str, sizeof(long_str), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(mixed, sizeof(mixed), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(mixed_map, sizeof(mixed_map), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(int_key, sizeof(int_key), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(truncated, sizeof(truncated), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ rc = test_parse_rep(nested, sizeof(nested), &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+
+ /* More properties than rep objects */
+ len = test_parse_encode_map(MYNEWT_VAL(OC_NUM_REP_OBJECTS) + 1);
+ rc = test_parse_rep(test_parse_buf, len, &rep);
+ TEST_ASSERT(rc != 0);
+ oc_free_rep(rep);
+}
+
+static void
+test_parse_rep_objects(void)
+{
+ /* [] */
+ static const uint8_t empty[] = { 0x80 };
+ /* [{"a": 1}, {"b": ["xy", "z"]}] */
+ static const uint8_t objs[] = { 0x82, 0xa1, 0x61, 'a', 0x01, 0xa1, 0x61,
+ 'b', 0x82, 0x62, 'x', 'y', 0x61, 'z' };
+ oc_rep_t *rep;
+ size_t len;
+ int rc;
+
+ rc = test_parse_rep(empty, sizeof(empty), &rep);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(rep == NULL);
+
+ rc = test_parse_rep(objs, sizeof(objs), &rep);
+ TEST_ASSERT_FATAL(rc == 0);
+ TEST_ASSERT_FATAL(rep != NULL && rep->type == OBJECT);
+ TEST_ASSERT_FATAL(rep->value_object != NULL);
+ TEST_ASSERT(rep->value_object->type == INT);
+ TEST_ASSERT(rep->value_object->value_int == 1);
+ TEST_ASSERT_FATAL(rep->next != NULL && rep->next->type == OBJECT);
+ TEST_ASSERT_FATAL(rep->next->value_object != NULL);
+ TEST_ASSERT_FATAL(rep->next->value_object->type == STRING_ARRAY);
+ TEST_ASSERT(oc_string_array_get_allocated_size(
+ rep->next->value_object->value_array) == 2);
+ TEST_ASSERT(!strcmp(
+ oc_string_array_get_item(rep->next->value_object->value_array, 0), "xy"));
+ TEST_ASSERT(!strcmp(
+ oc_string_array_get_item(rep->next->value_object->value_array, 1), "z"));
+ TEST_ASSERT(rep->next->next == NULL);
+ oc_free_rep(rep);
+
+ /* All rep objects were freed after failed and successful parsing */
+ len = test_parse_encode_map(MYNEWT_VAL(OC_NUM_REP_OBJECTS));
+ rc = test_parse_rep(test_parse_buf, len, &rep);
+ TEST_ASSERT(rc == 0);
+ oc_free_rep(rep);
+}
+
void
test_parse(void)
{
test_parse_coap_options();
test_parse_rep_arrays();
+ test_parse_rep_invalid();
+ test_parse_rep_objects();
}
(mynewt-core) 02/04: net/oic: Fix payload parsing in oc_parse_rep
Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 436f5ba541d462fa287b6ece586b5f88bdc6baa7
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 20:45:47 2026 +0100
net/oic: Fix payload parsing in oc_parse_rep
Payload parser did not validate decoded data and kept parsing after
errors, which could result in memory corruption or crashes:
- string array elements were copied into fixed size items without
checking length
- array element type was taken from each element, while array was
allocated based on the type of the first element, and object array
elements after a non-object first element dereferenced NULL pointer
- *out_rep was not set if payload root was neither a map nor an array,
and caller freed uninitialized pointer
- payload with root array was parsed as properties of the first array
element, which was not checked to be a map
- rep object and string allocation failures were not checked
- decoding errors were accumulated and ignored, which could result in
infinite loops on malformed payload
- out of memory error was truncated to 0 by uint16_t return type
Rewrite the parser so that every error is propagated and parsing stops
on first error. Arrays must contain elements of the same type, strings
must fit in string array items and nesting level of objects is now
limited. Root array is parsed as array of objects. *out_rep is always
set and tree parsed so far is linked to it, so it can be freed with
oc_free_rep() regardless of the result.
Assisted-by: Claude:claude-opus-5-5
---
net/oic/src/api/oc_rep.c | 509 ++++++++++++++++++++++++++++++++---------------
1 file changed, 344 insertions(+), 165 deletions(-)
diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c
index 2983f6881..ce939594b 100644
--- a/net/oic/src/api/oc_rep.c
+++ b/net/oic/src/api/oc_rep.c
@@ -70,10 +70,9 @@ _alloc_rep(void)
{
oc_rep_t *rep = os_memblock_get(&oc_rep_objects);
- memset(rep, 0, sizeof(*rep));
-#ifdef DEBUG
- oc_assert(rep != NULL);
-#endif
+ if (rep) {
+ memset(rep, 0, sizeof(*rep));
+ }
return rep;
}
@@ -134,189 +133,369 @@ oc_free_rep(oc_rep_t *rep)
the next pointer of the first object.
*/
-/* Parse single property */
-static void
-oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err)
+/* Maximum nesting level of objects in parsed payload */
+#define OC_REP_MAX_NESTING 8
+
+static CborError oc_parse_rep_object(CborValue *value, oc_rep_t **rep, int depth);
+
+/* Copies byte or text string value into newly allocated string */
+static CborError
+oc_parse_rep_string(CborValue *value, oc_string_t *str)
{
- size_t k, len;
- CborValue map, array;
- *rep = _alloc_rep();
- oc_rep_t *cur = *rep, **prev = 0;
- cur->next = 0;
- cur->value_object_array = 0;
- /* key */
- *err |= cbor_mbuf_value_calculate_string_length(value, &len);
- len++;
- oc_alloc_string(&cur->name, len);
- *err |= cbor_mbuf_value_copy_text_string(value, oc_string(cur->name), &len, NULL);
- *err |= cbor_mbuf_value_advance(value);
- /* value */
- switch (value->type) {
- case CborIntegerType:
- *err |= cbor_value_get_int64(value, &cur->value_int);
- cur->type = INT;
- break;
- case CborBooleanType:
- *err |= cbor_value_get_boolean(value, &cur->value_boolean);
- cur->type = BOOL;
- break;
- case CborDoubleType:
- *err |= cbor_value_get_double(value, &cur->value_double);
- cur->type = DOUBLE;
- break;
- case CborByteStringType:
- *err |= cbor_mbuf_value_calculate_string_length(value, &len);
- len++;
- oc_alloc_string(&cur->value_string, len);
- *err |= cbor_mbuf_value_copy_byte_string(
- value, (uint8_t *)oc_string(cur->value_string), &len, NULL);
- cur->type = BYTE_STRING;
- break;
- case CborTextStringType:
- *err |= cbor_mbuf_value_calculate_string_length(value, &len);
- len++;
- oc_alloc_string(&cur->value_string, len);
- *err |= cbor_mbuf_value_copy_text_string(value, oc_string(cur->value_string),
+ size_t len;
+ CborError err;
+
+ err = cbor_mbuf_value_calculate_string_length(value, &len);
+ if (err) {
+ return err;
+ }
+ /* Reserve space for terminating NUL, size must fit in os_sz */
+ if (len >= UINT16_MAX) {
+ return CborErrorDataTooLarge;
+ }
+ len++;
+
+ oc_alloc_string(str, len);
+ if (!oc_string(*str)) {
+ return CborErrorOutOfMemory;
+ }
+
+ if (cbor_value_is_text_string(value)) {
+ err = cbor_mbuf_value_copy_text_string(value, oc_string(*str), &len, NULL);
+ } else {
+ err = cbor_mbuf_value_copy_byte_string(value, (uint8_t *)oc_string(*str),
&len, NULL);
- cur->type = STRING;
- break;
- case CborMapType: /* when value is a map/object */ {
- oc_rep_t **obj = &cur->value_object; // object points to list of properties
- *err |= cbor_value_enter_container(value, &map);
- while (!cbor_value_at_end(&map)) {
- oc_parse_rep_value(&map, obj, err);
- (*obj)->next = 0;
- obj = &(*obj)->next;
- *err |= cbor_mbuf_value_advance(&map);
}
- cur->type = OBJECT;
- } break;
- case CborArrayType:
- *err |= cbor_value_enter_container(value, &array);
- len = 0;
- cbor_value_get_array_length(value, &len);
- if (len == 0) {
- CborValue t = array;
- while (!cbor_value_at_end(&t)) {
- len++;
- cbor_mbuf_value_advance(&t);
- }
+ if (err) {
+ return err;
}
- k = 0;
- while (!cbor_value_at_end(&array)) {
- switch (array.type) {
- case CborIntegerType:
- if (k == 0) {
- oc_new_int_array(&cur->value_array, len);
- cur->type = INT | ARRAY;
+
+ return cbor_mbuf_value_advance(value);
+}
+
+static CborError
+oc_parse_rep_string_array(CborValue *array, oc_rep_t *cur, size_t len,
+ oc_rep_value_type_t type)
+{
+ CborError err;
+ size_t slen;
+ size_t k;
+
+ oc_new_string_array(&cur->value_array, len);
+ if (!cur->value_array.oa_arr.s) {
+ return CborErrorOutOfMemory;
+ }
+ cur->type = type;
+
+ for (k = 0; k < len; k++) {
+ err = cbor_mbuf_value_calculate_string_length(array, &slen);
+ if (err) {
+ return err;
}
- *err |=
- cbor_value_get_int64(&array, oc_int_array(cur->value_array) + k);
- break;
- case CborDoubleType:
- if (k == 0) {
- oc_new_double_array(&cur->value_array, len);
- cur->type = DOUBLE | ARRAY;
+ /* Reserve space for terminating NUL */
+ if (slen >= STRING_ARRAY_ITEM_MAX_LEN) {
+ return CborErrorDataTooLarge;
}
- *err |=
- cbor_value_get_double(&array, oc_double_array(cur->value_array) + k);
- break;
- case CborBooleanType:
- if (k == 0) {
- oc_new_bool_array(&cur->value_array, len);
- cur->type = BOOL | ARRAY;
+ slen = STRING_ARRAY_ITEM_MAX_LEN;
+
+ if (type == STRING_ARRAY) {
+ err = cbor_mbuf_value_copy_text_string(
+ array, oc_string_array_get_item(cur->value_array, k), &slen, NULL);
+ } else {
+ err = cbor_mbuf_value_copy_byte_string(
+ array, (uint8_t *)oc_string_array_get_item(cur->value_array, k),
+ &slen, NULL);
}
- *err |=
- cbor_value_get_boolean(&array, oc_bool_array(cur->value_array) + k);
- break;
- case CborByteStringType:
- if (k == 0) {
- oc_new_string_array(&cur->value_array, len);
- cur->type = BYTE_STRING | ARRAY;
+ if (err) {
+ return err;
+ }
+
+ err = cbor_mbuf_value_advance(array);
+ if (err) {
+ return err;
+ }
+ }
+
+ return CborNoError;
+}
+
+static CborError
+oc_parse_rep_object_array(CborValue *array, oc_rep_t *cur, int depth)
+{
+ oc_rep_t **obj;
+ CborError err;
+
+ cur->type = OBJECT_ARRAY;
+ obj = &cur->value_object_array;
+
+ while (!cbor_value_at_end(array)) {
+ *obj = _alloc_rep();
+ if (!*obj) {
+ return CborErrorOutOfMemory;
+ }
+ (*obj)->type = OBJECT;
+
+ err = oc_parse_rep_object(array, &(*obj)->value_object, depth);
+ if (err) {
+ return err;
+ }
+
+ obj = &(*obj)->next;
+ }
+
+ return CborNoError;
+}
+
+static CborError
+oc_parse_rep_array(CborValue *value, oc_rep_t *cur, int depth)
+{
+ CborValue array;
+ CborValue t;
+ CborType type;
+ CborError err;
+ size_t len;
+ size_t k;
+
+ err = cbor_value_enter_container(value, &array);
+ if (err) {
+ return err;
+ }
+
+ /* All elements must be of the same type */
+ type = cbor_value_get_type(&array);
+ len = 0;
+ t = array;
+ while (!cbor_value_at_end(&t)) {
+ if (cbor_value_get_type(&t) != type) {
+ return CborErrorIllegalType;
+ }
+ err = cbor_mbuf_value_advance(&t);
+ if (err) {
+ return err;
}
- *err |= cbor_mbuf_value_calculate_string_length(&array, &len);
len++;
- *err |= cbor_mbuf_value_copy_byte_string(
- &array, (uint8_t *)oc_string_array_get_item(cur->value_array, k),
- &len, NULL);
+ }
+
+ switch (type) {
+ case CborIntegerType:
+ oc_new_int_array(&cur->value_array, len);
+ if (!cur->value_array.oa_arr.i) {
+ return CborErrorOutOfMemory;
+ }
+ cur->type = INT_ARRAY;
+ for (k = 0; k < len; k++) {
+ err = cbor_value_get_int64(&array, &cur->value_array.oa_arr.i[k]);
+ if (!err) {
+ err = cbor_mbuf_value_advance(&array);
+ }
+ if (err) {
+ return err;
+ }
+ }
break;
- case CborTextStringType:
- if (k == 0) {
- oc_new_string_array(&cur->value_array, len);
- cur->type = STRING | ARRAY;
+ case CborDoubleType:
+ oc_new_double_array(&cur->value_array, len);
+ if (!cur->value_array.oa_arr.d) {
+ return CborErrorOutOfMemory;
+ }
+ cur->type = DOUBLE_ARRAY;
+ for (k = 0; k < len; k++) {
+ err = cbor_value_get_double(&array, &cur->value_array.oa_arr.d[k]);
+ if (!err) {
+ err = cbor_mbuf_value_advance(&array);
+ }
+ if (err) {
+ return err;
+ }
}
- *err |= cbor_mbuf_value_calculate_string_length(&array, &len);
- len++;
- *err |= cbor_mbuf_value_copy_text_string(
- &array, (char *)oc_string_array_get_item(cur->value_array, k), &len, NULL);
break;
- case CborMapType:
- if (k == 0) {
- cur->type = OBJECT | ARRAY;
- cur->value_object_array = _alloc_rep();
- prev = &cur->value_object_array;
- } else {
- (*prev)->next = _alloc_rep();
- prev = &(*prev)->next;
+ case CborBooleanType:
+ oc_new_bool_array(&cur->value_array, len);
+ if (!cur->value_array.oa_arr.b) {
+ return CborErrorOutOfMemory;
}
- (*prev)->type = OBJECT;
- (*prev)->next = 0;
- oc_rep_t **obj = &(*prev)->value_object;
- /* Process a series of properties that make up an object of the array */
- *err |= cbor_value_enter_container(&array, &map);
- while (!cbor_value_at_end(&map)) {
- oc_parse_rep_value(&map, obj, err);
- obj = &(*obj)->next;
- *err |= cbor_mbuf_value_advance(&map);
+ cur->type = BOOL_ARRAY;
+ for (k = 0; k < len; k++) {
+ err = cbor_value_get_boolean(&array, &cur->value_array.oa_arr.b[k]);
+ if (!err) {
+ err = cbor_mbuf_value_advance(&array);
+ }
+ if (err) {
+ return err;
+ }
}
break;
- default:
+ case CborByteStringType:
+ err = oc_parse_rep_string_array(&array, cur, len, BYTE_STRING_ARRAY);
+ break;
+ case CborTextStringType:
+ err = oc_parse_rep_string_array(&array, cur, len, STRING_ARRAY);
break;
- }
- k++;
- *err |= cbor_mbuf_value_advance(&array);
+ case CborMapType:
+ err = oc_parse_rep_object_array(&array, cur, depth);
+ break;
+ default:
+ /* Empty array or unsupported element type, skip it */
+ return cbor_mbuf_value_advance(value);
}
- break;
- default:
- break;
- }
+ if (err) {
+ return err;
+ }
+
+ return cbor_value_leave_container(value, &array);
}
-uint16_t
-oc_parse_rep(struct os_mbuf *m, uint16_t payload_off,
- uint16_t payload_size, oc_rep_t **out_rep)
+/* Parse single property */
+static CborError
+oc_parse_rep_value(CborValue *value, oc_rep_t **rep, int depth)
+{
+ oc_rep_t *cur;
+ CborError err;
+
+ cur = _alloc_rep();
+ if (!cur) {
+ return CborErrorOutOfMemory;
+ }
+ *rep = cur;
+
+ /* key */
+ if (!cbor_value_is_text_string(value)) {
+ return CborErrorIllegalType;
+ }
+ err = oc_parse_rep_string(value, &cur->name);
+ if (err) {
+ return err;
+ }
+
+ /* value */
+ switch (cbor_value_get_type(value)) {
+ case CborIntegerType:
+ err = cbor_value_get_int64(value, &cur->value_int);
+ cur->type = INT;
+ break;
+ case CborBooleanType:
+ err = cbor_value_get_boolean(value, &cur->value_boolean);
+ cur->type = BOOL;
+ break;
+ case CborDoubleType:
+ err = cbor_value_get_double(value, &cur->value_double);
+ cur->type = DOUBLE;
+ break;
+ case CborByteStringType:
+ cur->type = BYTE_STRING;
+ return oc_parse_rep_string(value, &cur->value_string);
+ case CborTextStringType:
+ cur->type = STRING;
+ return oc_parse_rep_string(value, &cur->value_string);
+ case CborMapType:
+ cur->type = OBJECT;
+ return oc_parse_rep_object(value, &cur->value_object, depth);
+ case CborArrayType:
+ return oc_parse_rep_array(value, cur, depth);
+ default:
+ /* Unsupported value type, skip it */
+ break;
+ }
+ if (err) {
+ return err;
+ }
+
+ return cbor_mbuf_value_advance(value);
+}
+
+/* Parse map into list of properties */
+static CborError
+oc_parse_rep_object(CborValue *value, oc_rep_t **rep, int depth)
+{
+ CborValue map;
+ CborError err;
+
+ if (!cbor_value_is_map(value)) {
+ return CborErrorIllegalType;
+ }
+ if (depth >= OC_REP_MAX_NESTING) {
+ return CborErrorNestingTooDeep;
+ }
+
+ err = cbor_value_enter_container(value, &map);
+ if (err) {
+ return err;
+ }
+
+ while (!cbor_value_at_end(&map)) {
+ err = oc_parse_rep_value(&map, rep, depth + 1);
+ if (err) {
+ return err;
+ }
+ rep = &(*rep)->next;
+ }
+
+ return cbor_value_leave_container(value, &map);
+}
+
+static CborError
+oc_parse_rep_payload(struct os_mbuf *m, uint16_t payload_off, oc_rep_t **out_rep)
{
- CborParser parser;
- CborValue root_value, cur_value, map;
- CborError err = CborNoError;
- struct cbor_mbuf_reader br;
-
- cbor_mbuf_reader_init(&br, m, payload_off);
- err |= cbor_reader_parser_init(&br.r, &parser, &root_value);
- if (cbor_value_is_map(&root_value)) {
- err |= cbor_value_enter_container(&root_value, &cur_value);
- *out_rep = 0;
- oc_rep_t **cur = out_rep;
- while (cbor_value_is_valid(&cur_value)) {
- oc_parse_rep_value(&cur_value, cur, &err);
- err |= cbor_mbuf_value_advance(&cur_value);
- cur = &(*cur)->next;
+ CborParser parser;
+ CborValue root_value;
+ CborValue array;
+ CborError err;
+ struct cbor_mbuf_reader br;
+ oc_rep_t **cur;
+
+ *out_rep = NULL;
+
+ cbor_mbuf_reader_init(&br, m, payload_off);
+ err = cbor_reader_parser_init(&br.r, &parser, &root_value);
+ if (err) {
+ return err;
}
- } else if (cbor_value_is_array(&root_value)) {
- err |= cbor_value_enter_container(&root_value, &map);
- err |= cbor_value_enter_container(&map, &cur_value);
- *out_rep = 0;
- oc_rep_t **cur = out_rep;
- while (cbor_value_is_valid(&cur_value)) {
- *cur = _alloc_rep();
- (*cur)->type = OBJECT;
- oc_parse_rep_value(&cur_value, &(*cur)->value_object, &err);
- err |= cbor_mbuf_value_advance(&cur_value);
- (*cur)->next = 0;
- cur = &(*cur)->next;
+
+ if (cbor_value_is_map(&root_value)) {
+ return oc_parse_rep_object(&root_value, out_rep, 0);
}
- }
- return (uint16_t)err;
+
+ if (!cbor_value_is_array(&root_value)) {
+ return CborErrorIllegalType;
+ }
+
+ err = cbor_value_enter_container(&root_value, &array);
+ if (err) {
+ return err;
+ }
+
+ cur = out_rep;
+ while (!cbor_value_at_end(&array)) {
+ *cur = _alloc_rep();
+ if (!*cur) {
+ return CborErrorOutOfMemory;
+ }
+ (*cur)->type = OBJECT;
+
+ err = oc_parse_rep_object(&array, &(*cur)->value_object, 1);
+ if (err) {
+ return err;
+ }
+
+ cur = &(*cur)->next;
+ }
+
+ return CborNoError;
+}
+
+/*
+ * Parses payload into tree of oc_rep_t. Payload must be a map or an array of
+ * maps. On error *out_rep may point to partially parsed payload, it must be
+ * freed with oc_free_rep() regardless of returned value.
+ *
+ * Returns 0 on success, non-zero on failure.
+ */
+uint16_t
+oc_parse_rep(struct os_mbuf *m, uint16_t payload_off, uint16_t payload_size,
+ oc_rep_t **out_rep)
+{
+ /* CborError values do not fit in uint16_t */
+ return oc_parse_rep_payload(m, payload_off, out_rep) != CborNoError;
}
void