Reference re-parser: the subset of CommandLineToArgvW our builder emits (every * arg quote-wrapped; \" for embedded quotes; backslashes doubled before a quote). */
| 183 | /* Reference re-parser: the subset of CommandLineToArgvW our builder emits (every |
| 184 | * arg quote-wrapped; \" for embedded quotes; backslashes doubled before a quote). */ |
| 185 | static int parse_win_cmdline(const char *cmd, char out[][256], int max_args) { |
| 186 | int argc = 0; |
| 187 | const char *p = cmd; |
| 188 | while (*p) { |
| 189 | while (*p == ' ' || *p == '\t') { |
| 190 | p++; |
| 191 | } |
| 192 | if (!*p || argc >= max_args) { |
| 193 | break; |
| 194 | } |
| 195 | char *o = out[argc]; |
| 196 | size_t oi = 0; |
| 197 | bool in_quotes = false; |
| 198 | /* Guard every write: each out[] row is 256 bytes; test args stay well under |
| 199 | * that, but cap defensively so a future longer arg fails a length assertion |
| 200 | * rather than smashing the stack. */ |
| 201 | #define PUTO(ch) \ |
| 202 | do { \ |
| 203 | if (oi < 255) { \ |
| 204 | o[oi++] = (ch); \ |
| 205 | } \ |
| 206 | } while (0) |
| 207 | for (;;) { |
| 208 | size_t nbs = 0; |
| 209 | while (*p == '\\') { |
| 210 | nbs++; |
| 211 | p++; |
| 212 | } |
| 213 | if (*p == '"') { |
| 214 | for (size_t k = 0; k < nbs / 2; k++) { |
| 215 | PUTO('\\'); |
| 216 | } |
| 217 | if (nbs % 2) { |
| 218 | PUTO('"'); /* odd run → the quote is an escaped literal */ |
| 219 | } else { |
| 220 | in_quotes = !in_quotes; /* even run → the quote is a delimiter */ |
| 221 | } |
| 222 | p++; |
| 223 | } else { |
| 224 | for (size_t k = 0; k < nbs; k++) { |
| 225 | PUTO('\\'); |
| 226 | } |
| 227 | if (*p == '\0' || (!in_quotes && (*p == ' ' || *p == '\t'))) { |
| 228 | break; |
| 229 | } |
| 230 | PUTO(*p); |
| 231 | p++; |
| 232 | } |
| 233 | } |
| 234 | #undef PUTO |
| 235 | o[oi] = '\0'; |
| 236 | argc++; |
| 237 | } |
| 238 | return argc; |
| 239 | } |
| 240 | |
| 241 | static bool cmdline_roundtrips(const char *const *argv) { |
| 242 | char cmd[4096]; |
no outgoing calls
no test coverage detected