Normalize an untrusted repository-relative path without touching the * filesystem. Absolute paths, drive/UNC paths, control bytes, and any `..` * component are rejected. A root scope (`.`) normalizes to the empty prefix. */
| 6842 | * filesystem. Absolute paths, drive/UNC paths, control bytes, and any `..` |
| 6843 | * component are rejected. A root scope (`.`) normalizes to the empty prefix. */ |
| 6844 | static coverage_path_result_t coverage_normalize_rel(const char *input, bool allow_root, char *out, |
| 6845 | size_t out_size) { |
| 6846 | if (!input || !out || out_size == 0U) { |
| 6847 | return COVERAGE_PATH_INVALID; |
| 6848 | } |
| 6849 | out[0] = '\0'; |
| 6850 | size_t len = strlen(input); |
| 6851 | if (len == 0U || len >= out_size || input[0] == '/' || input[0] == '\\' || |
| 6852 | (len >= 2U && isalpha((unsigned char)input[0]) && input[1] == ':')) { |
| 6853 | return COVERAGE_PATH_OUTSIDE; |
| 6854 | } |
| 6855 | |
| 6856 | size_t in = 0U; |
| 6857 | size_t written = 0U; |
| 6858 | while (in < len) { |
| 6859 | while (in < len && (input[in] == '/' || input[in] == '\\')) { |
| 6860 | in++; |
| 6861 | } |
| 6862 | if (in >= len) { |
| 6863 | break; |
| 6864 | } |
| 6865 | size_t start = in; |
| 6866 | while (in < len && input[in] != '/' && input[in] != '\\') { |
| 6867 | unsigned char c = (unsigned char)input[in]; |
| 6868 | if (c < 0x20U) { |
| 6869 | return COVERAGE_PATH_INVALID; |
| 6870 | } |
| 6871 | in++; |
| 6872 | } |
| 6873 | size_t part_len = in - start; |
| 6874 | if (part_len == 1U && input[start] == '.') { |
| 6875 | continue; |
| 6876 | } |
| 6877 | if (part_len == 2U && input[start] == '.' && input[start + 1U] == '.') { |
| 6878 | return COVERAGE_PATH_OUTSIDE; |
| 6879 | } |
| 6880 | if (written > 0U) { |
| 6881 | if (written + 1U >= out_size) { |
| 6882 | return COVERAGE_PATH_INVALID; |
| 6883 | } |
| 6884 | out[written++] = '/'; |
| 6885 | } |
| 6886 | if (written + part_len >= out_size) { |
| 6887 | return COVERAGE_PATH_INVALID; |
| 6888 | } |
| 6889 | memcpy(out + written, input + start, part_len); |
| 6890 | written += part_len; |
| 6891 | } |
| 6892 | out[written] = '\0'; |
| 6893 | return written > 0U || allow_root ? COVERAGE_PATH_OK : COVERAGE_PATH_INVALID; |
| 6894 | } |
| 6895 | |
| 6896 | static const char *coverage_path_freshness(cbm_store_t *store, const char *project, |
| 6897 | const char *root_path, const char *rel_path, |
no outgoing calls
no test coverage detected