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 15:02:09 UTC

(mynewt-core) 06/07: libc/baselibc: Add tests for strndup, strnstr and sscanf

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 5dc31532777ba4ab31f2549bb36adc192d02a65c
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 23:23:27 2026 +0100

    libc/baselibc: Add tests for strndup, strnstr and sscanf
    
    Verify that strndup() does not write past the allocated buffer when n
    is larger than string length, that strnstr() limits the search to len
    characters of haystack and that sscanf() %[ conversion honours field
    width and includes both ends of a character range. These cover the
    bugs fixed in previous commits.
    
    Assisted-by: Claude:claude-opus-5-5
---
 libc/baselibc/selftest/src/baselibc_test.c         |  6 ++
 libc/baselibc/selftest/src/testcases/string_test.c | 83 ++++++++++++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/libc/baselibc/selftest/src/baselibc_test.c b/libc/baselibc/selftest/src/baselibc_test.c
index 6fbb6c970..da818c393 100755
--- a/libc/baselibc/selftest/src/baselibc_test.c
+++ b/libc/baselibc/selftest/src/baselibc_test.c
@@ -22,10 +22,16 @@
 #include "testutil/testutil.h"
 
 TEST_CASE_DECL(tinyprintf_test)
+TEST_CASE_DECL(strndup_test)
+TEST_CASE_DECL(strnstr_test)
+TEST_CASE_DECL(sscanf_set_test)
 
 TEST_SUITE(baselibc_test_suite)
 {
     tinyprintf_test();
+    strndup_test();
+    strnstr_test();
+    sscanf_set_test();
 }
 
 int
diff --git a/libc/baselibc/selftest/src/testcases/string_test.c b/libc/baselibc/selftest/src/testcases/string_test.c
new file mode 100644
index 000000000..c96aaba37
--- /dev/null
+++ b/libc/baselibc/selftest/src/testcases/string_test.c
@@ -0,0 +1,83 @@
+/*
+ * 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 <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "testutil/testutil.h"
+
+TEST_CASE_SELF(strndup_test)
+{
+    char src[16] = "abc";
+    char *s;
+
+    /* n larger than string length */
+    s = strndup(src, 10);
+    TEST_ASSERT_FATAL(s != NULL);
+    TEST_ASSERT(!strcmp(s, "abc"));
+    free(s);
+
+    /* n smaller than string length */
+    s = strndup("abcdef", 3);
+    TEST_ASSERT_FATAL(s != NULL);
+    TEST_ASSERT(!strcmp(s, "abc"));
+    free(s);
+
+    s = strndup("abc", 0);
+    TEST_ASSERT_FATAL(s != NULL);
+    TEST_ASSERT(!strcmp(s, ""));
+    free(s);
+}
+
+TEST_CASE_SELF(strnstr_test)
+{
+    const char *h = "abcdef";
+
+    TEST_ASSERT(strnstr(h, "cd", 6) == h + 2);
+    TEST_ASSERT(strnstr(h, "cd", 4) == h + 2);
+    /* Needle not fully within first len characters */
+    TEST_ASSERT(strnstr(h, "cd", 3) == NULL);
+    TEST_ASSERT(strnstr(h, "ef", 6) == h + 4);
+}
+
+TEST_CASE_SELF(sscanf_set_test)
+{
+    struct {
+        char buf[4];
+        char guard[8];
+    } s;
+    char buf[16];
+    int rc;
+
+    /* Range includes both ends */
+    rc = sscanf("az-", "%[a-z]", buf);
+    TEST_ASSERT(rc == 1);
+    TEST_ASSERT(!strcmp(buf, "az"));
+
+    /* Trailing '-' is literal */
+    rc = sscanf("a-b", "%[a-]", buf);
+    TEST_ASSERT(rc == 1);
+    TEST_ASSERT(!strcmp(buf, "a-"));
+
+    /* Width limits number of characters stored */
+    memset(&s, 0x5a, sizeof(s));
+    rc = sscanf("123456789", "%3[0-9]", s.buf);
+    TEST_ASSERT(rc == 1);
+    TEST_ASSERT(!strcmp(s.buf, "123"));
+    TEST_ASSERT(s.guard[0] == 0x5a);
+}