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-mcumgr.git

commit 3f34acfb33e009a947df5737dbdcc3c3e285c0da
Author: Szymon Janc <[email protected]>
AuthorDate: Wed Oct 7 15:56:36 2026 +0200

    cborattr/test: Add test for string array decoding
    
    This adds test that verifies that string array elements not fitting in
    destination store are rejected and nothing is written past the store.
    
    Assisted-by: Claude:claude-opus-5-5
---
 cborattr/test/src/test_cborattr.c                  |   1 +
 cborattr/test/src/test_cborattr.h                  |   1 +
 .../cborattr_decode_string_array_bounds.c          | 102 +++++++++++++++++++++
 3 files changed, 104 insertions(+)

diff --git a/cborattr/test/src/test_cborattr.c 
b/cborattr/test/src/test_cborattr.c
index 4840f5d..e340628 100644
--- a/cborattr/test/src/test_cborattr.c
+++ b/cborattr/test/src/test_cborattr.c
@@ -36,6 +36,7 @@ TEST_SUITE(test_cborattr_suite)
     test_cborattr_decode_substring_key();
     test_cborattr_decode_nesting();
     test_cborattr_decode_string_bounds();
+    test_cborattr_decode_string_array_bounds();
 }
 
 #if MYNEWT_VAL(SELFTEST)
diff --git a/cborattr/test/src/test_cborattr.h 
b/cborattr/test/src/test_cborattr.h
index 67437ca..d423f25 100644
--- a/cborattr/test/src/test_cborattr.h
+++ b/cborattr/test/src/test_cborattr.h
@@ -52,6 +52,7 @@ TEST_CASE_DECL(test_cborattr_decode_unnamed_array);
 TEST_CASE_DECL(test_cborattr_decode_substring_key);
 TEST_CASE_DECL(test_cborattr_decode_nesting);
 TEST_CASE_DECL(test_cborattr_decode_string_bounds);
+TEST_CASE_DECL(test_cborattr_decode_string_array_bounds);
 
 #ifdef __cplusplus
 }
diff --git a/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c 
b/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c
new file mode 100644
index 0000000..b0a2ce4
--- /dev/null
+++ b/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "test_cborattr.h"
+
+static uint8_t test_arr_buf[64];
+static int test_arr_len;
+
+static int
+test_arr_wr(struct cbor_encoder_writer *cew, const char *data, int len)
+{
+    memcpy(test_arr_buf + test_arr_len, data, len);
+    test_arr_len += len;
+    assert(test_arr_len < sizeof(test_arr_buf));
+    return 0;
+}
+
+/* Encodes {"a": [strs...]} */
+static void
+test_arr_encode(const char **strs, int cnt)
+{
+    struct cbor_encoder_writer writer = { .write = test_arr_wr };
+    CborEncoder enc;
+    CborEncoder map;
+    CborEncoder arr;
+    int i;
+
+    test_arr_len = 0;
+    cbor_encoder_init(&enc, &writer, 0);
+    cbor_encoder_create_map(&enc, &map, CborIndefiniteLength);
+    cbor_encode_text_stringz(&map, "a");
+    cbor_encoder_create_array(&map, &arr, CborIndefiniteLength);
+    for (i = 0; i < cnt; i++) {
+        cbor_encode_text_stringz(&arr, strs[i]);
+    }
+    cbor_encoder_close_container(&map, &arr);
+    cbor_encoder_close_container(&enc, &map);
+}
+
+TEST_CASE_SELF(test_cborattr_decode_string_array_bounds)
+{
+    const char *fit[] = { "ab", "cd" };
+    const char *no_fit[] = { "abcdefgh", "zzzzzzzzzzzz", "zzzzzzzzzzzz" };
+    struct {
+        char store[8];
+        char guard[24];
+    } buf;
+    char *ptrs[3];
+    int cnt;
+    int rc;
+    int i;
+
+    const struct cbor_attr_t attrs[] = {
+        {
+            .attribute = "a",
+            .type = CborAttrArrayType,
+            .addr.array.element_type = CborAttrTextStringType,
+            .addr.array.arr.strings.ptrs = ptrs,
+            .addr.array.arr.strings.store = buf.store,
+            .addr.array.arr.strings.storelen = sizeof(buf.store),
+            .addr.array.count = &cnt,
+            .addr.array.maxlen = 3,
+        },
+        { 0 },
+    };
+
+    /* Strings fitting in store are decoded. */
+    test_arr_encode(fit, 2);
+    rc = cbor_read_flat_attrs(test_arr_buf, test_arr_len, attrs);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(cnt == 2);
+    TEST_ASSERT(strcmp(ptrs[0], "ab") == 0);
+    TEST_ASSERT(strcmp(ptrs[1], "cd") == 0);
+
+    /* String not fitting in store is rejected and nothing is written past
+     * the store.
+     */
+    memset(buf.guard, 0x5a, sizeof(buf.guard));
+    test_arr_encode(no_fit, 3);
+    rc = cbor_read_flat_attrs(test_arr_buf, test_arr_len, attrs);
+    TEST_ASSERT(rc != 0);
+    TEST_ASSERT(cnt == 0);
+    for (i = 0; i < sizeof(buf.guard); i++) {
+        TEST_ASSERT_FATAL(buf.guard[i] == 0x5a);
+    }
+}

Reply via email to