Lexically normalizes an absolute path without consulting the filesystem. * Rovo may point at a config file that does not exist yet, so realpath-style * canonicalization is not available. Traversal is rejected rather than * resolved, and Windows aliases that trim trailing dots/spaces are rejected. */
| 132 | * canonicalization is not available. Traversal is rejected rather than |
| 133 | * resolved, and Windows aliases that trim trailing dots/spaces are rejected. */ |
| 134 | static int agent_normalize_absolute_path(const char *input, bool windows, char *output, |
| 135 | size_t output_size) { |
| 136 | if (!input || !input[0] || !output || output_size == 0U) { |
| 137 | return -1; |
| 138 | } |
| 139 | size_t read = 0U; |
| 140 | size_t written = 0U; |
| 141 | bool unc = false; |
| 142 | if (windows) { |
| 143 | if (isalpha((unsigned char)input[0]) && input[1] == ':' && |
| 144 | agent_path_separator(input[2], true)) { |
| 145 | if (output_size < 4U) { |
| 146 | return -1; |
| 147 | } |
| 148 | output[written++] = (char)toupper((unsigned char)input[0]); |
| 149 | output[written++] = ':'; |
| 150 | output[written++] = '/'; |
| 151 | read = 3U; |
| 152 | } else if (agent_path_separator(input[0], true) && agent_path_separator(input[1], true)) { |
| 153 | if (output_size < 3U) { |
| 154 | return -1; |
| 155 | } |
| 156 | output[written++] = '/'; |
| 157 | output[written++] = '/'; |
| 158 | read = 2U; |
| 159 | unc = true; |
| 160 | } else { |
| 161 | return -1; |
| 162 | } |
| 163 | } else { |
| 164 | if (input[0] != '/' || output_size < 2U) { |
| 165 | return -1; |
| 166 | } |
| 167 | output[written++] = '/'; |
| 168 | read = 1U; |
| 169 | } |
| 170 | |
| 171 | size_t components = 0U; |
| 172 | while (input[read]) { |
| 173 | while (input[read] && agent_path_separator(input[read], windows)) { |
| 174 | read++; |
| 175 | } |
| 176 | if (!input[read]) { |
| 177 | break; |
| 178 | } |
| 179 | size_t start = read; |
| 180 | while (input[read] && !agent_path_separator(input[read], windows)) { |
| 181 | read++; |
| 182 | } |
| 183 | size_t length = read - start; |
| 184 | if (length == 1U && input[start] == '.') { |
| 185 | continue; |
| 186 | } |
| 187 | if (length == 2U && input[start] == '.' && input[start + 1U] == '.') { |
| 188 | return -1; |
| 189 | } |
| 190 | if (windows) { |
| 191 | if (input[start + length - 1U] == '.' || input[start + length - 1U] == ' ') { |
no test coverage detected