|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2026, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2026-06-09 OrbisAI Add regression test for V-004 snprintf fix |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Test Case Name: tty_ptmx Buffer Bounds Regression Test |
| 13 | + * |
| 14 | + * Test Objectives: |
| 15 | + * - Verify that the snprintf-based device name formatting in |
| 16 | + * lwp_ptmx_init() never writes beyond the allocated buffer. |
| 17 | + * - Guard against regression to the original sprintf overflow (V-004). |
| 18 | + * |
| 19 | + * Test Scenarios: |
| 20 | + * - Normal input: root_path + "/ptmx" fits within the allocated buffer. |
| 21 | + * - Oversized input: combined length exceeds the buffer; snprintf must |
| 22 | + * truncate without overflowing. |
| 23 | + * - Canary bytes placed immediately after the buffer must remain intact. |
| 24 | + * |
| 25 | + * Verification Metrics: |
| 26 | + * - Output buffer byte at index [buf_size] must equal the canary value. |
| 27 | + * - snprintf return value must be < buf_size when input fits. |
| 28 | + * - snprintf return value must be >= buf_size when input is oversized |
| 29 | + * (snprintf reports what would have been written). |
| 30 | + * |
| 31 | + * Dependencies: |
| 32 | + * - Software configuration: RT_USING_SMART must be enabled. |
| 33 | + * |
| 34 | + * Expected Results: |
| 35 | + * - Final output: "[ PASSED ] [ result ] testcase (testcases.lwp.tty_ptmx.buffer_bounds)" |
| 36 | + * - No assertion failures during test execution. |
| 37 | + */ |
| 38 | + |
| 39 | +#include "utest_assert.h" |
| 40 | + |
| 41 | +#include <rtdef.h> |
| 42 | +#include <string.h> |
| 43 | +#include <stdio.h> |
| 44 | + |
| 45 | +#define DEV_REL_PATH "/ptmx" |
| 46 | +#define CANARY 0xAB |
| 47 | + |
| 48 | +static void tty_ptmx_buffer_bounds_tc(void) |
| 49 | +{ |
| 50 | + /* Simulate the allocation and snprintf call from lwp_ptmx_init(). */ |
| 51 | + struct |
| 52 | + { |
| 53 | + const char *root_path; |
| 54 | + int should_fit; /* 1 = expect no truncation */ |
| 55 | + } cases[] = { |
| 56 | + { "/dev", 1 }, |
| 57 | + { "/dev/pts", 1 }, |
| 58 | + { "/", 1 }, |
| 59 | + /* Oversized: root alone is longer than reasonable device name */ |
| 60 | + { "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0 }, |
| 61 | + }; |
| 62 | + |
| 63 | + for (rt_size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) |
| 64 | + { |
| 65 | + const char *root_path = cases[i].root_path; |
| 66 | + rt_size_t root_len = strlen(root_path); |
| 67 | + rt_size_t buf_size = root_len + sizeof(DEV_REL_PATH); /* mirrors kernel alloc */ |
| 68 | + |
| 69 | + /* Allocate one extra byte as a canary to detect overflow. */ |
| 70 | + char *raw = (char *)rt_malloc(buf_size + 1); |
| 71 | + uassert_not_null(raw); |
| 72 | + |
| 73 | + raw[buf_size] = (char)CANARY; |
| 74 | + |
| 75 | + int written = snprintf(raw, buf_size, "%s%s", root_path, DEV_REL_PATH); |
| 76 | + |
| 77 | + /* Canary must be intact regardless of input size. */ |
| 78 | + uassert_int_equal((unsigned char)raw[buf_size], CANARY); |
| 79 | + |
| 80 | + if (cases[i].should_fit) |
| 81 | + { |
| 82 | + /* snprintf returns number of chars that would be written (excl. NUL). */ |
| 83 | + uassert_true(written >= 0 && (rt_size_t)written < buf_size); |
| 84 | + /* Result must be null-terminated. */ |
| 85 | + uassert_int_equal(raw[written], '\0'); |
| 86 | + } |
| 87 | + |
| 88 | + rt_free(raw); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static rt_err_t utest_tc_init(void) |
| 93 | +{ |
| 94 | + return RT_EOK; |
| 95 | +} |
| 96 | + |
| 97 | +static rt_err_t utest_tc_cleanup(void) |
| 98 | +{ |
| 99 | + return RT_EOK; |
| 100 | +} |
| 101 | + |
| 102 | +static void testcase(void) |
| 103 | +{ |
| 104 | + UTEST_UNIT_RUN(tty_ptmx_buffer_bounds_tc); |
| 105 | +} |
| 106 | +UTEST_TC_EXPORT(testcase, "testcases.lwp.tty_ptmx.buffer_bounds", utest_tc_init, utest_tc_cleanup, 10); |
0 commit comments