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/26 12:40:48 UTC

(mynewt-core) 06/07: encoding/cborattr: Check array element types

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 54a0fdcd83a231491ad6480877562b93be7dae9c
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 19:59:43 2026 +0100

    encoding/cborattr: Check array element types
    
    Array elements were passed to tinycbor getters without checking their
    type, and the first element was read even if the array was empty.
    tinycbor getters assert on type mismatch, so an empty array or an
    element of unexpected type resulted in an assert. For text string
    arrays this was the case only when decoding from a flat buffer, as
    the mbuf reader returns an error instead.
    
    Stop at the end of the array and return CborErrorIllegalType if an
    element type does not match the array element type. Empty arrays are
    now decoded with element count set to 0.
    
    Also decode elements of float arrays with cbor_value_get_float(), as
    cbor_value_get_double() asserted on every float element.
    
    Assisted-by: Claude:claude-opus-5-5
---
 encoding/cborattr/src/cborattr.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c
index 1449b97f0..80cace05a 100644
--- a/encoding/cborattr/src/cborattr.c
+++ b/encoding/cborattr/src/cborattr.c
@@ -405,6 +405,15 @@ cbor_read_array(struct CborValue *value, const struct cbor_array_t *arr)
     arrcount = 0;
     tp = arr->arr.strings.store;
     for (off = 0; off < arr->maxlen; off++) {
+        if (cbor_value_at_end(&elem)) {
+            break;
+        }
+        /* Element type is checked by cbor_internal_read_object() */
+        if (arr->element_type != CborAttrStructObjectType &&
+            !valid_attr_type(cbor_value_get_type(&elem), arr->element_type)) {
+            err |= CborErrorIllegalType;
+            break;
+        }
         switch (arr->element_type) {
         case CborAttrBooleanType:
             lptr = &arr->arr.booleans.store[off];
@@ -427,7 +436,13 @@ cbor_read_array(struct CborValue *value, const struct cbor_array_t *arr)
             lptr = &arr->arr.halffloats.store[off];
             err |= cbor_value_get_half_float(&elem, lptr);
             break;
-        case CborAttrFloatType:
+        case CborAttrFloatType: {
+            float fval;
+
+            err |= cbor_value_get_float(&elem, &fval);
+            arr->arr.reals.store[off] = fval;
+            break;
+        }
         case CborAttrDoubleType:
             lptr = &arr->arr.reals.store[off];
             err |= cbor_value_get_double(&elem, lptr);
@@ -469,9 +484,6 @@ cbor_read_array(struct CborValue *value, const struct cbor_array_t *arr)
                 break;
             }
         }
-        if (!cbor_value_is_valid(&elem)) {
-            break;
-        }
     }
     if (arr->count) {
         *arr->count = arrcount;